
366
|
附录
E
然而,如果之前定义了
x
,我们会得到旧的定义:
In [12]: x = 1
In [13]: foo()
In [14]: x
Out [14]: 1
这些与内置函数和方法相关。如果你不小心重写了它们,从那一刻之后你都不能再使用它
们了。所以,如果你重写特殊的词列表(
list
)或日期(
date
),拥有这些名字的内置函数
不会在剩余的代码中正常执行(或者从那一刻之后):
In [17]: from
datetime
import date
In [19]: date(2015, 2, 5)
Out[19]: datetime.date(2015, 2, 5)
In [20]: date = 'my date obj'
In [21]: date(2015, 2, 5)
.---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-105-7f129d4341d0> in <module>()
----> 1 date(2015, 2, 5)
TypeError: 'str' object is not callable
正如你所见,使用共享名称的变量(或与任何其他标准
Python
命名空间或你使用的任何其
他库共享名称)可能造成调试的噩梦。如果你在代码中使用特殊的名称,并且意识到固有 ...