Chinaunix

标题: [Python基础]如何判断一个变量是否存在 [打印本页]

作者: wzhuzhu    时间: 2009-11-27 16:23
标题: [Python基础]如何判断一个变量是否存在
在Python中如何判断一个变量是否已经存在?
谢谢!
作者: leo_ss_pku_cu    时间: 2009-11-27 16:54
if yourvariable:
    blablabal
作者: zfzaizheli    时间: 2009-11-27 17:01
不晓得。。关注

我一般是定义None 再用if判断 呵呵~  很傻瓜的办法~~
作者: wzhuzhu    时间: 2009-11-27 18:50
原帖由 leo_ss_pku_cu 于 2009-11-27 16:54 发表
if yourvariable:
    blablabal


你这个方式有问题
# python
Python 2.6 (r26:66714, Jun  8 2009, 16:07:26)
[GCC 4.4.0 20090506 (Red Hat 4.4.0-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a="b"
>>> if a: print a
...
b
>>> if b: print b
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined

还是会出现问题。

谢谢你的回复!
作者: liubingqian    时间: 2009-11-27 21:19
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************
   
IDLE 1.2.4      
>>> dir()
['__builtins__', '__doc__', '__name__']
>>> 's' in dir()
False
>>> s = 'hello, python'
>>> 's' in dir()
True
>>>
作者: nietsche    时间: 2009-11-27 21:22
不知hasattr(object,name),是否可满足你的要求.
比如你定义一个类(与之类似就可):
class Test:
      t = 1

if hasattr(Test,'t'):
    print t


......
作者: mjus    时间: 2009-11-27 23:06
try:
     whatever
except NameError:
     print "whatever not defined"
作者: emacsnw    时间: 2009-11-28 03:08
原帖由 wzhuzhu 于 2009-11-27 00:23 发表
在Python中如何判断一个变量是否已经存在?
谢谢!


5楼正解。
作者: openspace    时间: 2009-11-28 05:16
搜了一下
http://www.cnblogs.com/starspace/archive/2008/12/03/1347007.html
python中检测某个变量是否有定义

第一种方法:
      'var'   in   locals().keys()

第二种方法:
    try:
         print   var
    except   NameError:
         print   'var   not   defined'

第三种方法:
      'var'   in   dir()

作者: wzhuzhu    时间: 2009-11-28 16:04
原帖由 openspace 于 2009-11-28 05:16 发表
搜了一下

多谢大家的回复。
感谢,问题解决!
作者: wzhuzhu    时间: 2009-11-28 16:04
原帖由 liubingqian 于 2009-11-27 21:19 发表
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

    ************************************ ...

多谢回复。测试OK!
问题解决
作者: Northland    时间: 2009-11-30 15:34
标题: 回复 #11 wzhuzhu 的帖子
直接用内置函数 vars()

s = 'test var'

if vars().has_key('s') :
    ....
作者: smallfish_xy    时间: 2009-11-30 15:38
原帖由 Northland 于 2009-11-30 15:34 发表
直接用内置函数 vars()

s = 'test var'

if vars().has_key('s') :
    ....

其实判断locals,globals,和vars都可以
作者: baby_xiong12    时间: 2009-12-04 12:13
帮5楼完善下:
lo='s' in dir()
if not lo:
     print 'no var'
else:
     print 'var is exist'




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2