does a variable exist
Wm. King
wjk at wjk.mv.com
Sat Jan 8 16:13:57 EST 2000
Thanks -- was just wondering if there was
more overhead in catching/throwing exceptions...
(still learning) (c;{
Darrell wrote:
> "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
More information about the Python-list
mailing list