>>> def foo(bar=None):
... if bar is None: # 或者用 if not bar:
... bar = []
... bar.append("baz")
... return bar
...
>>> foo()
["baz"]
>>> foo()
["baz"]
>>> foo()
["baz"]>>> def foo(bar=None):
... if bar is None: # 或者用 if not bar:
... bar = []
... bar.append("baz")
... return bar
...
>>> foo()
["baz"]
>>> foo()
["baz"]
>>> foo()
["baz"]
常見錯誤2:不正確的使用類變量
看下面一個例子: Python
>>> class A(object):
... x = 1
...
>>> class B(A):
... pass
...
>>> class C(A):
... pass
...
>>> print A.x, B.x, C.x
1 1 1>>> class A(object):
... x = 1
...
>>> class B(A):
... pass
...
>>> class C(A):
... pass
...
>>> print A.x, B.x, C.x
1 1 1
在Python里,類變量通常在內(nèi)部被當(dāng)做字典來處理并遵循通常所說的方法解析順序(Method Resolution Order (MRO))。因此在上面的代碼中,因為屬性x在類C中找不到,因此它會往上去它的基類中查找(在上面的例子中只有A這個類,當(dāng)然Python是支持多重繼承(multiple inheritance)的)。換句話說,C沒有它自己獨立于A的屬性x。因此對C.x的引用實際上是對A.x的引用。(B.x不是對A.x的引用是因為在第二步里B.x=2將B.x引用到了2這個對象上,倘若沒有如此,B.x仍然是引用到A.x上的。——譯者注)
常見錯誤3:在異常處理時錯誤的使用參數(shù)
假設(shè)你有如下的代碼: Python
>>> try:
... l = ["a", "b"]
... int(l[2])
... except ValueError, IndexError: # 想捕捉兩個異常
... pass
...
Traceback (most recent call last):File "<stdin>", line 3, in <module>
IndexError: list index out of range>>> try:
... l = ["a", "b"]
... int(l[2])
... except ValueError, IndexError: # 想捕捉兩個異常
... pass
...
Traceback (most recent call last):File "<stdin>", line 3, in <module>
IndexError: list index out of range
>>> x = 10
>>> def foo():
... x += 1
... print x
...
>>> foo()
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "<stdin>", line 2, in foo
UnboundLocalError: local variable 'x' referenced before assignment>>> x = 10
>>> def foo():
... x += 1
... print x
...
>>> foo()
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "<stdin>", line 2, in foo
UnboundLocalError: local variable 'x' referenced before assignment
>>> odd = lambda x : bool(x % 2)
>>> numbers = [n for n in range(10)]
>>> for i in range(len(numbers)):
... if odd(numbers[i]):
... del numbers[i] # 這不對的:在遍歷列表時刪掉列表的元素。
...
Traceback (most recent call last):File "<stdin>", line 2, in <module>
IndexError: list index out of range
>>> odd = lambda x : bool(x % 2)
>>> numbers = [n for n in range(10)]
>>> for i in range(len(numbers)):
... if odd(numbers[i]):
... del numbers[i] # 這不對的:在遍歷列表時刪掉列表的元素。
...
Traceback (most recent call last):File "<stdin>", line 2, in <module>
IndexError: list index out of range
>>> odd = lambda x : bool(x % 2)
>>> numbers = [n for n in range(10)]
>>> numbers[:] = [n for n in numbers if not odd(n)] # 啊,這多優(yōu)美
>>> numbers
[0, 2, 4, 6, 8]>>> odd = lambda x : bool(x % 2)
>>> numbers = [n for n in range(10)]
>>> numbers[:] = [n for n in numbers if not odd(n)] # 啊,這多優(yōu)美
>>> numbers
[0, 2, 4, 6, 8]
常見錯誤6:搞不清楚在閉包(closures)中Python是怎樣綁定變量的
看這個例子: Python
>>> def create_multipliers():
... return [lambda x : i * x for i in range(5)]
>>> for multiplier in create_multipliers():
... print multiplier(2)
...
1
2
3
4
5>>> def create_multipliers():
... return [lambda x : i * x for i in range(5)]
>>> for multiplier in create_multipliers():
... print multiplier(2)
...
>>> import b
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "b.py", line 1, in <module>import aFile "a.py", line 6, in <module>print f()File "a.py", line 4, in freturn b.x
AttributeError: 'module' object has no attribute 'x'
1
2
3
4
5
6
7
8
9
10>>> import b
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "b.py", line 1, in <module>import aFile "a.py", line 6, in <module>print f()File "a.py", line 4, in freturn b.x
AttributeError: 'module' object has no attribute 'x'