Test the existence of a variable?

Discussion in 'Python' started by Thierry S., Sep 3, 2004.

  1. Thierry S.

    Thierry S. Guest

    Hello.

    I would to test the existence of a variable before to use it (like
    isset($myVar) in PHP).
    I try using "if myVar: ", but there is the error meesage (naturally):
    "NameError: name 'myVar' is not defined"

    Please, could you tell me what for function exist to test the variable with
    Python?

    Regards,
    --
    Thierry S.
    Thierry S., Sep 3, 2004
    #1
    1. Advertising

  2. Thierry S.

    Robin Becker Guest

    Thierry S. wrote:

    > Hello.
    >
    > I would to test the existence of a variable before to use it (like
    > isset($myVar) in PHP).
    > I try using "if myVar: ", but there is the error meesage (naturally):
    > "NameError: name 'myVar' is not defined"
    >
    > Please, could you tell me what for function exist to test the variable with
    > Python?
    >
    > Regards,

    if globals().has_key('myVar'):
    ....

    --
    Robin Becker
    Robin Becker, Sep 3, 2004
    #2
    1. Advertising

  3. Thierry S.

    Bernd Kaiser Guest

    Robin Becker wrote:

    > Thierry S. wrote:
    >
    >> Hello.
    >>
    >> I would to test the existence of a variable before to use it (like
    >> isset($myVar) in PHP).
    >> I try using "if myVar: ", but there is the error meesage (naturally):
    >> "NameError: name 'myVar' is not defined"
    >>
    >> Please, could you tell me what for function exist to test the variable with
    >> Python?
    >>
    >> Regards,

    > if globals().has_key('myVar'):
    > ....
    >


    I was once told, that in Python you do something and live with the
    consequences.

    try:
    print var
    except NameError, e:
    print e
    Bernd Kaiser, Sep 3, 2004
    #3
  4. Thierry S.

    Jp Calderone Guest

    Robin Becker wrote:
    > Thierry S. wrote:
    >
    >> Hello.
    >>
    >> I would to test the existence of a variable before to use it (like
    >> isset($myVar) in PHP).
    >> I try using "if myVar: ", but there is the error meesage (naturally):
    >> "NameError: name 'myVar' is not defined"
    >>
    >> Please, could you tell me what for function exist to test the variable
    >> with
    >> Python?
    >>
    >> Regards,

    >
    > if globals().has_key('myVar'):
    > ....
    >



    >>> def f(x):

    .... if globals().has_key('x'):
    .... print 'x is', x
    .... else:
    .... print 'x is not defined'
    ....
    >>> f(10)

    x is not defined

    Before anyone points out locals(), I know all about it.

    Testing for the existence of a variable is something to avoid doing.
    Variables should be bound unconditionally. There is always a sentinel
    value that can be used as a place-holder.

    Instead of:

    # ...
    if not isset('x'):
    # initialize x
    # use x

    use:

    x = None
    # ...
    if x is None:
    # initialize x
    # use x

    If None is a valid value for x to take on, pick another sentinel.
    Even better is to pick a legitimate and useful value and just write the
    whole thing as:

    x = default
    # use x

    Often this is not possible... but often it is.

    If it is really necessary to check for the existence of a variable by
    name, for example if the variable name taken from user input, then you
    may actually wish to use a dict, instead of variables in your local scope:

    d = {}
    # Get user input, store it in x
    if x not in d:
    d[x] = initialize()
    # use d[x]

    Jp


    Jp
    Jp Calderone, Sep 3, 2004
    #4
  5. Thierry S.

    Peter Hansen Guest

    Bernd Kaiser wrote:

    > I was once told, that in Python you do something and live with the
    > consequences.
    >
    > try:
    > print var
    > except NameError, e:
    > print e


    Much easier to live with the consequences if there are none:

    try:
    var
    except NameError, e:
    print e

    In other words, merely evaluate the name and catch the exception,
    rather than trying to print it!

    -Peter
    Peter Hansen, Sep 3, 2004
    #5
  6. Thierry S.

    Dan Gass Guest

    "Thierry S." <> wrote in message news:<ch9hhf$4re$>...
    > Hello.
    >
    > I would to test the existence of a variable before to use it (like
    > isset($myVar) in PHP).
    > I try using "if myVar: ", but there is the error meesage (naturally):
    > "NameError: name 'myVar' is not defined"
    >
    > Please, could you tell me what for function exist to test the variable with
    > Python?
    >
    > Regards,


    You may want to look at using a try/except block, for example:

    try:
    print myVar
    except NameError:
    print "myVar does not exist"

    </dan>
    Dan Gass, Sep 3, 2004
    #6
    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. bjam
    Replies:
    0
    Views:
    387
  2. Matt
    Replies:
    14
    Views:
    2,261
    Rolf Magnus
    Feb 22, 2005
  3. Replies:
    9
    Views:
    4,706
    Ancient_Hacker
    Oct 26, 2006
  4. zino

    how to test cache existence

    zino, Jan 23, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    567
    Alvin Bruney [ASP.NET MVP]
    Jan 24, 2008
  5. Vmusic
    Replies:
    6
    Views:
    122
Loading...

Share This Page