find isset() php function equivalent in python

Discussion in 'Python' started by Olivier Noblanc ATOUSOFT, Feb 1, 2005.

  1. Hello


    What is the equivalent function of php isset() in python

    Thank you very much.

    olivier noblanc
    http://www.logiciel-erp.fr
     
    Olivier Noblanc ATOUSOFT, Feb 1, 2005
    #1
    1. Advertising

  2. Olivier Noblanc ATOUSOFT

    Ola Natvig Guest

    Olivier Noblanc ATOUSOFT wrote:
    > Hello
    >
    >
    > What is the equivalent function of php isset() in python
    >
    > Thank you very much.
    >
    > olivier noblanc
    > http://www.logiciel-erp.fr
    >
    >


    try:
    if variable:
    # isset
    pass
    except NameError:
    # not set
    pass

    could work...

    --
    --------------------------------------
    Ola Natvig <>
    infoSense AS / development
     
    Ola Natvig, Feb 1, 2005
    #2
    1. Advertising

  3. Olivier Noblanc ATOUSOFT

    Max M Guest

    Ola Natvig wrote:
    > Olivier Noblanc ATOUSOFT wrote:
    >
    >> Hello
    >>
    >>
    >> What is the equivalent function of php isset() in python

    >
    > try:
    > if variable:
    > # isset
    > pass
    > except NameError:
    > # not set
    > pass
    >


    you could use:

    >>> 'variable' in vars()


    But be aware that it is bad bad practice to do it like that.

    If you need variables that you don't know that name of, you should put
    them in a dictionary. They are made for that exact purpose.

    >>> unkown_vars = {}
    >>> unkown_vars['variable'] = 42
    >>> 'variable' in unkown_vars

    True


    --

    hilsen/regards Max M, Denmark

    http://www.mxm.dk/
    IT's Mad Science
     
    Max M, Feb 1, 2005
    #3
  4. Olivier Noblanc ATOUSOFT

    Ola Natvig Guest

    Max M wrote:
    > Ola Natvig wrote:
    >
    >> Olivier Noblanc ATOUSOFT wrote:
    >>
    >>> Hello
    >>>
    >>>
    >>> What is the equivalent function of php isset() in python

    >>
    >>
    >> try:
    >> if variable:
    >> # isset
    >> pass
    >> except NameError:
    >> # not set
    >> pass
    >>

    >
    >
    > you could use:
    >
    > >>> 'variable' in vars()

    >
    > But be aware that it is bad bad practice to do it like that.
    >
    > If you need variables that you don't know that name of, you should put
    > them in a dictionary. They are made for that exact purpose.
    >
    > >>> unkown_vars = {}
    > >>> unkown_vars['variable'] = 42
    > >>> 'variable' in unkown_vars

    > True
    >
    >


    If it's a greater possibility that the 'variable' are set than it's not
    you will get better performance when using:

    try:
    print unknown_vars['variable']
    except KeyError:
    print 'variable are not set'

    istead of

    if 'variable' in unknown_vars:
    print unknown_vars['variable']
    else:
    print 'variable are not set'

    You could even use

    print unknown_vars.get('variable', 'variable are not set')

    dictionary.get(key, default) returns the default if key are not located
    in the dictionary, I'm not sure if the function uses the try / except
    KeyError aproach or what it uses.

    --
    --------------------------------------
    Ola Natvig <>
    infoSense AS / development
     
    Ola Natvig, Feb 1, 2005
    #4
    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. Patrick Fitzsimmons

    Does a function like isset() exist in Python?

    Patrick Fitzsimmons, Jun 23, 2005, in forum: Python
    Replies:
    6
    Views:
    1,333
    Roy Smith
    Jun 23, 2005
  2. Tim Leslie
    Replies:
    0
    Views:
    757
    Tim Leslie
    Jun 23, 2005
  3. Rajive Narain
    Replies:
    0
    Views:
    2,066
    Rajive Narain
    Sep 18, 2009
  4. Alexey Tafintsev

    How !isset in Ruby

    Alexey Tafintsev, Jun 15, 2008, in forum: Ruby
    Replies:
    20
    Views:
    852
    Alexey Tafintsev
    Jun 16, 2008
  5. juglesh
    Replies:
    5
    Views:
    232
    Thomas 'PointedEars' Lahn
    May 15, 2005
Loading...

Share This Page