How to check the exists of a name?

Discussion in 'Python' started by StarWing, Oct 18, 2009.

  1. StarWing

    StarWing Guest

    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?
    StarWing, Oct 18, 2009
    #1
    1. Advertising

  2. StarWing

    Chris Rebert Guest

    On Sat, Oct 17, 2009 at 11:30 PM, 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?


    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
    Chris Rebert, Oct 18, 2009
    #2
    1. Advertising

  3. StarWing

    StarWing Guest

    On 10月18æ—¥, 下åˆ2æ—¶37分, Chris Rebert <> wrote:
    > On Sat, Oct 17, 2009 at 11:30 PM, 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?

    >
    > 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 :)
    StarWing, Oct 18, 2009
    #3
  4. StarWing

    Chris Rebert Guest

    On Sat, Oct 17, 2009 at 11:43 PM, StarWing <> wrote:
    > On 10月18æ—¥, 下åˆ2æ—¶37分, Chris Rebert <> wrote:
    >> On Sat, Oct 17, 2009 at 11:30 PM, 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?

    >>
    >> 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
    Chris Rebert, Oct 18, 2009
    #4
  5. 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
    Steven D'Aprano, Oct 18, 2009
    #5
  6. 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
    Steven D'Aprano, Oct 18, 2009
    #6
  7. >>>>> David <> (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 <>
    WWW: http://pietvanoostrum.com/
    PGP key: [8DAE142BE17999C4]
    Piet van Oostrum, Oct 19, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Onur Bozkurt

    Check if e-mail exists...

    Onur Bozkurt, Aug 29, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    27,223
    php-dev
    Apr 19, 2009
  2. Totan
    Replies:
    0
    Views:
    898
    Totan
    Apr 17, 2006
  3. Jason Stacy
    Replies:
    2
    Views:
    4,640
    Roedy Green
    Apr 27, 2008
  4. Ulf Meinhardt
    Replies:
    8
    Views:
    6,089
  5. JDP@Work
    Replies:
    1
    Views:
    124
    JDP@Work
    Dec 19, 2005
Loading...

Share This Page