如题所述
vars(不带参数的形式)和locals这两个内建函数返回一个当前scope内定义的所有局部变量的字典。
>>> 'a' in vars()
False
>>> a = 1
>>> 'a' in vars()
True
>>> b = 2
>>> locals()
>>> 'a' in vars()
False
>>> a = 1
>>> 'a' in vars()
True
>>> b = 2
>>> locals()
温馨提示:答案为网友推荐,仅供参考
第1个回答 2014-02-26
自定义了一个isset函数.
def isset(v):
try:
type (eval(v))
except:
return 0
else:
return 1
函数调用.
if isset('user_name'):
print 'user_name is defined'
else
print 'user_name is not defined'
def isset(v):
try:
type (eval(v))
except:
return 0
else:
return 1
函数调用.
if isset('user_name'):
print 'user_name is defined'
else
print 'user_name is not defined'