Possible Duplicate:
Easy way to check that variable is defined in python?
How do I check if a variable exists in Python?可能的重复:检查在python中定义的变量的简单方法?如何检查Python中是否存在变量?
How do you know whether a variable has been set at a particular place in the code at runtime? This is not always obvious because (1) the variable could be conditionally set, and (2) the variable could be conditionally deleted. I'm looking for something like defined()
in Perl or isset()
in PHP or defined?
in Ruby.
如何知道在运行时在代码中的特定位置是否设置了变量?这并不总是显而易见的,因为(1)变量可以有条件地设置,(2)变量可以有条件地删除。我正在寻找像Perl中的define()或PHP中的isset()或所定义的?在Ruby中。
if condition:
a = 42
# is "a" defined here?
if other_condition:
del a
# is "a" defined here?
7 个解决方案
#1
483
try:
thevariable
except NameError:
print "well, it WASN'T defined after all!"
else:
print "sure, it was defined."
#2
253
'a' in vars() or 'a' in globals()
'a in vars()或'a' in globals()
if you want to be pedantic, you can check the builtins too'a' in vars(__builtins__)
如果你想学究式,你可以在vars(__builtins__)中检查建筑的“a”
#3
98
I think it's better to avoid the situation. It's cleaner and clearer to write:
我认为最好避免这种情况。写得更清晰:
a = None
if condition:
a = 42
#4
11
try:
a # does a exist in the current namespace
except NameError:
a = 10 # nope
#5
5
One possible situation where this might be needed:
一种可能需要这样做的情况:
If you are using finally
block to close connections but in the try
block, the program exits with sys.exit()
before the connection is defined. In this case, the finally
block will be called and the connection closing statement will fail since no connection was created.
如果您正在使用finally块来关闭连接,但是在try块中,程序在定义连接之前使用sys.exit()退出。在这种情况下,最终块将被调用,连接关闭语句将失败,因为没有创建连接。
#6
4
For this particular case it's better to do a = None
instead of del a
. This will decrement reference count to object a
was (if any) assigned to and won't fail when a
is not defined. Note, that del
statement doesn't call destructor of an object directly, but unbind it from variable. Destructor of object is called when reference count became zero.
对于这个特殊的情况,最好使用a = None而不是del a,这样会减少赋给对象a的引用计数(如果有的话),并且在没有定义a时不会失败。注意,del语句并不直接调用对象的析构函数,而是将其与变量绑定。当引用计数为零时,调用对象的析构函数。
#7
-3
If one wants to catch attempts to access a not-defined variable inside an object, there is a very easy way of doing that:
如果想要捕获访问对象内部未定义变量的尝试,有一种非常简单的方法:
class Whatever(object):
def __getattr__(self, key):
return None
Here, python first tries to find an attribute within the object or the object tree, and only if that fails the __getattr__(self, key)
function is called. This means, if __getattr__
is called we can simply return None
.
在这里,python首先尝试在对象或对象树中找到一个属性,只有当它失败时,才调用__getattr__(self, key)函数。这意味着,如果调用__getattr__,我们可以返回None。
#1
483
try:
thevariable
except NameError:
print "well, it WASN'T defined after all!"
else:
print "sure, it was defined."
#2
253
'a' in vars() or 'a' in globals()
'a in vars()或'a' in globals()
if you want to be pedantic, you can check the builtins too'a' in vars(__builtins__)
如果你想学究式,你可以在vars(__builtins__)中检查建筑的“a”
#3
98
I think it's better to avoid the situation. It's cleaner and clearer to write:
我认为最好避免这种情况。写得更清晰:
a = None
if condition:
a = 42
#4
11
try:
a # does a exist in the current namespace
except NameError:
a = 10 # nope
#5
5
One possible situation where this might be needed:
一种可能需要这样做的情况:
If you are using finally
block to close connections but in the try
block, the program exits with sys.exit()
before the connection is defined. In this case, the finally
block will be called and the connection closing statement will fail since no connection was created.
如果您正在使用finally块来关闭连接,但是在try块中,程序在定义连接之前使用sys.exit()退出。在这种情况下,最终块将被调用,连接关闭语句将失败,因为没有创建连接。
#6
4
For this particular case it's better to do a = None
instead of del a
. This will decrement reference count to object a
was (if any) assigned to and won't fail when a
is not defined. Note, that del
statement doesn't call destructor of an object directly, but unbind it from variable. Destructor of object is called when reference count became zero.
对于这个特殊的情况,最好使用a = None而不是del a,这样会减少赋给对象a的引用计数(如果有的话),并且在没有定义a时不会失败。注意,del语句并不直接调用对象的析构函数,而是将其与变量绑定。当引用计数为零时,调用对象的析构函数。
#7
-3
If one wants to catch attempts to access a not-defined variable inside an object, there is a very easy way of doing that:
如果想要捕获访问对象内部未定义变量的尝试,有一种非常简单的方法:
class Whatever(object):
def __getattr__(self, key):
return None
Here, python first tries to find an attribute within the object or the object tree, and only if that fails the __getattr__(self, key)
function is called. This means, if __getattr__
is called we can simply return None
.
在这里,python首先尝试在对象或对象树中找到一个属性,只有当它失败时,才调用__getattr__(self, key)函数。这意味着,如果调用__getattr__,我们可以返回None。