学术相关
Real‐time demands, restaurant density, and delivery reliability: an empirical analysis of on‐demand meal delivery 1
Consumers prefer products that work using directionally consistent causal chains2
Probabilistic Machine Learning: New Frontiers for Modeling Consumers and their Choices3
事件序列建模——霍克斯过程
技术技巧
前端丨 8 个超级好玩的鼠标光标效果
Python丨云朵君:生产级Python代码风格
括号技巧
括号解包元组
1
2
3
4
5
6
meats = ["chicken", "fish"]
(
first_meat_of_the_day,
second_meat_of_the_day
) = meats
括号组合字符串
括号内字符串文字会自动连接,无需+运算符。
1
2
3
4
5
6
error_log_message = (
"ERROR. Failed ..."
f"{code1}: {description1}"
f"reason: {reason}"
"-----------------"
)
#### 括号进行多方法链接
1
2
3
4
5
6
7
8
9
# 传统方法
x = obj.method1().method2().method3()
# 使用括号
test_api = (
obj.first_chained_method()
.second_chained_method()
.third_chained_method()
)
括号索引嵌套字典
在生产环境下,由于嵌套层级更多,键名更长,对于嵌套字典的索引通常难以在一行完成,使用括号进行多行索引会更清晰。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 传统方法
d = {
"data" : {
"num": 1
}
}
x = d["data"]["num"]
# 生产环境下
result = (
my_dict["dictionary_key1"]
["dictionary_key2"]
["dictionary_key3"]
)
括号复杂布尔条件
对于非常复杂的条件应编写函数。
1
2
3
4
5
6
7
8
if (
long_conditional_statement_1
and
long_conditional_statement_2
and
long_conditional_statement_3
):
do_something()
多行列表推导式
1
2
3
4
5
resultant_list_after_transform = [
some_transformation(element)
for element in some_previous_iterable
if element not in some_set
]
减少缩进级别
在生产级代码中,缩进级别可能很多,因此应尽可能减少缩进级别。
1
2
3
4
for element in some_list:
if not conditiion:
continue
do_something()
防止None值
在访问一些对象属性,特别是嵌套属性时应该防止None值报错。
1
2
3
4
5
6
7
8
if (
dog
and
dog.owner
and
dog.owner.name == "bob"
):
do_something()
迭代中也应防止迭代None值。
1
2
for element in mylist or []:
do_something(element)
若mylist为None则返回[]。
内部函数/变量
最好以_开头。
常用功能装饰器
-
Li, X., Wang, X., Liu, Z., Zhang, J., & Tang, J. (2024). Real‐time demands, restaurant density, and delivery reliability: An empirical analysis of on‐demand meal delivery. Journal of Operations Management, joom.1339. https://doi.org/10.1002/joom.1339 ↩
-
Bharti, S., & Sussman, A. B. (2024). Consumers prefer products that work using directionally consistent causal chains. Journal of Consumer Research, ucae066. https://doi.org/10.1093/jcr/ucae066 ↩
-
Dew R., Padilla N., Luo L. E., Oblander S., Ansari A., Boughanmi K., Braun M., Feinberg F., Liu J., Otter T., Tian L., Wang Y., & Yin M. (2024). Probabilistic Machine Learning: New Frontiers for Modeling Consumers and their Choices. International Journal of Research in Marketing. https://doi.org/10.1016/j.ijresmar.2024.11.002 ↩