Call variable as a function

Discussion in 'Python' started by Chmouel Boudjnah, Mar 3, 2005.

  1. Hey,

    It's look simple for most of the people here but i can't figure out to that.

    If i have :

    def test():
    print "foo"

    and:

    var = "test"

    how do i call function test from var, kind of ${$var} in some others
    languages (or eval())

    Cheers, Chmouel.
    Chmouel Boudjnah, Mar 3, 2005
    #1
    1. Advertising

  2. Chmouel Boudjnah

    Steve Holden Guest

    Chmouel Boudjnah wrote:
    > Hey,
    >
    > It's look simple for most of the people here but i can't figure out to
    > that.
    >
    > If i have :
    >
    > def test():
    > print "foo"
    >
    > and:
    >
    > var = "test"
    >
    > how do i call function test from var, kind of ${$var} in some others
    > languages (or eval())
    >

    There's always a way:

    >>> def test():

    .... print "hello"
    ....
    >>> var = "test"
    >>> eval("%s()" % var)

    hello

    But it depends how you are creating the reference to the function. The
    above is required if all you have is a string, but it would also be
    possible to set the variable to the function rather than the function's
    name:

    >>> var = test
    >>> var()

    hello
    >>>


    Hope this helps.

    regards
    Steve
    --
    Meet the Python developers and your c.l.py favorites March 23-25
    Come to PyCon DC 2005 http://www.pycon.org/
    Steve Holden http://www.holdenweb.com/
    Steve Holden, Mar 3, 2005
    #2
    1. Advertising

  3. Chmouel Boudjnah

    Steve Holden Guest

    Chmouel Boudjnah wrote:
    > Steve Holden wrote:
    >
    >> But it depends how you are creating the reference to the function. The
    >> above is required if all you have is a string, but it would also be
    >> possible to set the variable to the function rather than the
    >> function's name
    >> >>> var = test
    >> >>> var()

    >> hello
    >> >>>

    >> Hope this helps.

    >
    >
    > Thanks man it does help.
    >

    Then it might help even more to realize that you can use lists and
    dictionaries of functions as well:

    >>> def f1(x):

    .... return x*2
    ....
    >>> def f2(x):

    .... return "This is an %s" % x
    ....
    >>> def f3(x):

    .... return "%s:%s" % (x, x)
    ....
    >>> fl = [f1, f2, f3]
    >>> for s in ("bigstring", "tiny"):

    .... for i in range(len(fl)):
    .... print fl(s)
    ....
    bigstringbigstring
    This is an bigstring
    bigstring:bigstring
    tinytiny
    This is an tiny
    tiny:tiny
    >>>


    regards
    Steve
    --
    Steve Holden +1 703 861 4237 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/
    Steve Holden, Mar 3, 2005
    #3
  4. Chmouel Boudjnah

    Nick Coghlan Guest

    Steve Holden wrote:
    > There's always a way:
    >
    > >>> def test():

    > ... print "hello"
    > ...
    > >>> var = "test"
    > >>> eval("%s()" % var)

    > hello


    I'd go with locals() for the simple case of a name lookup:

    Py> def test():
    .... print "Hi there!"
    ....
    Py> var = "test"
    Py> locals()[var]()
    Hi there!

    Cheers,
    Nick.

    --
    Nick Coghlan | | Brisbane, Australia
    ---------------------------------------------------------------
    http://boredomandlaziness.skystorm.net
    Nick Coghlan, Mar 4, 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. grbgooglefan
    Replies:
    2
    Views:
    423
    Pascal Bourguignon
    Jan 30, 2008
  2. grbgooglefan
    Replies:
    4
    Views:
    442
    Kenny McCormack
    Jan 30, 2008
  3. grbgooglefan
    Replies:
    0
    Views:
    392
    grbgooglefan
    Jan 30, 2008
  4. Alok
    Replies:
    3
    Views:
    250
  5. THAKUR PRASHANT SINGH

    Class Function call vs Normal Function call

    THAKUR PRASHANT SINGH, Feb 26, 2010, in forum: Ruby
    Replies:
    7
    Views:
    187
    THAKUR PRASHANT SINGH
    Feb 27, 2010
Loading...

Share This Page