其实加或者不加本身并没有那么重要,一致就好。
比较神奇的是 python。
根据 pep8,等号两边通常是要留一个空格的,但是有一个例外:
Don't use spaces around the = sign when used to indicate a keyword argument or a default parameter value.
Yes:
def complex(real, imag=0.0):
return magic(r=real, i=imag)
No:
def complex(real, imag = 0.0):
return magic(r = real, i = imag)
然而这例外本身又有例外:
When combining an argument annotation with a default value, use spaces around the = sign (but only for those arguments that have both an annotation and a default).
Yes:
def munge(sep: AnyStr = None): ...
def munge(input: AnyStr, sep: AnyStr = None, limit=1000): ...
No:
def munge(input: AnyStr=None): ...
def munge(input: AnyStr, limit = 1000): ...
这种宁可为了『利用空格来增加视觉可见性』小 trick,而不惜复杂化规则,破坏整体一致性的小家子做派,说实话,真是让人有点受不了……