Re: How to check the exists of a name?

2009-10-19 Thread Piet van Oostrum
 David 71da...@libero.it (D) wrote:

D Il Sat, 17 Oct 2009 23:43:36 -0700 (PDT), StarWing ha scritto:
 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 :-)

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.
-- 
Piet van Oostrum p...@vanoostrum.org
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


How to check the exists of a name?

2009-10-18 Thread 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?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check the exists of a name?

2009-10-18 Thread Chris Rebert
On Sat, Oct 17, 2009 at 11:30 PM, StarWing weasley...@sina.com wrote:
 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
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check the exists of a name?

2009-10-18 Thread David
Il Sat, 17 Oct 2009 23:30:02 -0700 (PDT), StarWing ha scritto:

 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?

Just use it in a try..except block.

try:
print myname
except NameError:
print myname does not exists


D.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check the exists of a name?

2009-10-18 Thread StarWing
On 10月18日, 下午2时37分, Chris Rebert c...@rebertia.com wrote:
 On Sat, Oct 17, 2009 at 11:30 PM, StarWing weasley...@sina.com wrote:
  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
 --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 :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check the exists of a name?

2009-10-18 Thread David
Il Sat, 17 Oct 2009 23:43:36 -0700 (PDT), StarWing ha scritto:

 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 :-)

locals().has_key(myname) 
globals().has_key(myname)

D.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check the exists of a name?

2009-10-18 Thread Chris Rebert
On Sat, Oct 17, 2009 at 11:43 PM, StarWing weasley...@sina.com wrote:
 On 10月18日, 下午2时37分, Chris Rebert c...@rebertia.com wrote:
 On Sat, Oct 17, 2009 at 11:30 PM, StarWing weasley...@sina.com wrote:
  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.

 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
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check the exists of a name?

2009-10-18 Thread Ben Finney
David 71da...@libero.it writes:

 Il Sat, 17 Oct 2009 23:43:36 -0700 (PDT), StarWing ha scritto:

  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 :-)

 locals().has_key(myname) 
 globals().has_key(myname)

The ‘dict.has_key’ method is deprecated, and is removed in Python 3.

The ‘in’ operator now works on dict objects:

'foo' in locals()
'foo' in globals()

-- 
 \ “We must become the change we want to see.” —Mahatma Gandhi |
  `\   |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check the exists of a name?

2009-10-18 Thread Steven D'Aprano
On Sat, 17 Oct 2009 23:30:02 -0700, StarWing wrote:

 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()




-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check the exists of a name?

2009-10-18 Thread Steven D'Aprano
On Sat, 17 Oct 2009 23:54:37 -0700, Chris Rebert wrote:

 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.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list