does a variable exist
Darrell
news at dorb.com
Fri Jan 7 21:46:39 EST 2000
More information about the Python-list mailing list
Fri Jan 7 21:46:39 EST 2000
- Previous message (by thread): does a variable exist
- Next message (by thread): does a variable exist
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
"Wm. King" <wjk at wjk.mv.com> wrote in message news:38767A50.CFDEC3C2 at wjk.mv.com... > Could you do something like: > > try: > print x > except NameError: > print "No global or local x" > > Just wondering? > Yulp. You see that a lot for imports also. try: from _socket import * except ImportError: from socket import * Throwing exceptions takes time. Not a problem for imports but might be a problem when accessing variables. Here's another way to say the same thing. >>> myVar=1 >>> import sys >>> globals().get('myVar',None) 1 >>> globals().get('xxxx',None) >>> >>> class VarError: ... def __init__(self, msg): ... self._msg=msg ... def __str__(self): ... return self._msg ... >>> >>> print globals().get('xxxx',VarError("No global or local xxxx")) No global or local xxxx >>> -- --Darrell
- Previous message (by thread): does a variable exist
- Next message (by thread): does a variable exist
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list