我有一些相当长(~150个字符)的django查询.将它们分成多行的首选方法是什么?
例如(不,不是我的真实代码):
编辑:更改了示例,因为人们专注于重复过滤,而不是查询的长度:
person = models.UzbekistaniCitizen.objects.filter(occupation__income__taxable__gte=40000).exclude(face__eyes__color=blue).order_by('height').select_related('siblings', 'children')
Run Code Online (Sandbox Code Playgroud)
以下是我能想到的两种方式:
使用反斜杠作为换行符:
person = models.UzbekistaniCitizen.objects.\
filter(occupation__income__taxable__gte=40000).\
exclude(face__eyes__color=blue).\
order_by('height').\
select_related('siblings', 'children')
Run Code Online (Sandbox Code Playgroud)在新行中重新应用过滤器:
person = models.UzbekistaniCitizen.objects
person = person.(occupation__income__taxable__gte=40000)
person = person.exclude(face__eyes__color=blue)
person = person.order_by('height')
person = person.select_related('siblings', 'children')
Run Code Online (Sandbox Code Playgroud)我已经很好地掌握了我的第一种编程语言C语言.我知道合理数量的技巧和技巧,并编写了不少程序,主要是用于科学的东西.现在我想分支并理解OOP,Python似乎是一个很好的方向.
我已经看到了几个关于如何学习Python的问题,但是大多数问题都来自那些第一次开始编程的人.我不需要一个可以告诉我字符串是什么的教程,但我确实需要一个可以告诉我如何在Python中创建字符串的教程.对一些好的资源有任何帮助可以查看吗?如果来源是免费的奖励积分:)
我正在研究一个需要对整数进行分解的Project Euler问题.我可以得出所有素数的列表,这些素数是给定数字的因子.算术的基本定理意味着我可以使用此列表来推导出数字的每个因素.
我目前的计划是将每个数字放在基本素数列表中并提高其功效,直到它不再是一个整数因子来找到每个素数的最大指数.然后,我将乘以素数指数对的每个可能组合.
例如,对于180:
Given: prime factors of 180: [2, 3, 5]
Find maximum exponent of each factor:
180 / 2^1 = 90
180 / 2^2 = 45
180 / 2^3 = 22.5 - not an integer, so 2 is the maximum exponent of 2.
180 / 3^1 = 60
180 / 3^2 = 20
180 / 3^3 = 6.6 - not an integer, so 2 is the maximum exponent of 3.
180 / 5^1 = 36
180 …Run Code Online (Sandbox Code Playgroud) 试试这个:
A = 1
B = 2
C = A + B
def main():
global C
print A
Run Code Online (Sandbox Code Playgroud)
输出main()是1.
为什么是这样?为什么要main了解用于评估的其他全局变量C?