How to check the exists of a name?

S

StarWing

Sometimes I want to make a simple flags. and i need to check there is
a name in current scope or not (that is, we can visit this name, no
matter where is it). and how to do that in python?
 
C

Chris Rebert

Sometimes I want to make a simple flags. and i need to check there is
a name in current scope or not (that is, we can visit this name, no
matter where is it). and how to do that in python?

You should avoid needing to do that in the first place. Common
alternatives include using a dict instead of variables, depending on
your circumstances.

Cheers,
Chris
 
S

StarWing

You should avoid needing to do that in the first place. Common
alternatives include using a dict instead of variables, depending on
your circumstances.

Cheers,
Chris
--http://blog.rebertia.com

Okay... Thank you... But if I want to do that, what shall I do?

I got a idea, use a try...except statement. there are another way to
do it ?

(I just curious now, because I solve my problem in another way :)
 
C

Chris Rebert

Okay... Thank you... But if I want to do that, what shall I do?

I got a idea, use a try...except statement. there are another way to
do it ?

(I just curious now, because I solve my problem in another way :)

Perhaps if you could explain why there's the possibility these
variables might not be defined...

Cheers,
Chris
 
S

Steven D'Aprano

Sometimes I want to make a simple flags. and i need to check there is a
name in current scope or not (that is, we can visit this name, no matter
where is it). and how to do that in python?

(1) Use a sentinel:

myname = None # always exists
....
# much later
if myname is None:
print "initialising myname"
else:
process(myname)


(2) Try it and see:

try:
myname
except NameError:
print "myname does not exist"
else:
print "myname exists"


(3) Look Before You Leap:

# inside a class:
'myname' in self.__dict__ or self.__class__.__dict__

# better, because it uses inheritance and supports __slots__:
hasattr(self, 'myname')

# inside a function
'myname' in locals()
'myname' in globals()
 
S

Steven D'Aprano

Perhaps if you could explain why there's the possibility these variables
might not be defined...

If I have to support older versions of Python:

try:
bin
except NameError:
# Define my own.
def bin(arg):
...

But for my own variables? No way. I just make sure they're defined.
 
P

Piet van Oostrum

David said:
D> Il Sat, 17 Oct 2009 23:43:36 -0700 (PDT), StarWing ha scritto:
D> locals().has_key(myname)
D> globals().has_key(myname)

1. This excludes builtins. The OP says: `no matter where is it', so that
suggests also builins.
2. The more modern invocation would be "myname in locals()" or "myname
in globals()". In Python3 the other one isn't supported anymore.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top