您如何知道变量是否已在运行时在代码中的特定位置设置?这并不总是显而易见的,因为(1)变量可以有条件地设置,(2)变量可以有条件地删除.我正在寻找像defined()Perl isset(),PHP或defined?Ruby中的东西.
if condition:
a = 42
# is "a" defined here?
if other_condition:
del a
# is "a" defined here?
Run Code Online (Sandbox Code Playgroud) 我看到python 3.2在functools库中有memoization作为装饰器. http://docs.python.org/py3k/library/functools.html#functools.lru_cache
不幸的是,它尚未向后移植到2.7.是否有任何具体原因导致它在2.7中不可用?是否有任何第三方图书馆提供相同的功能,还是应该自己编写?