Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Call variable as a function

Reply
Thread Tools

Call variable as a function

 
 
Chmouel Boudjnah
Guest
Posts: n/a
 
      03-03-2005
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.

 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      03-03-2005
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/
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      03-03-2005
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[i](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/
 
Reply With Quote
 
Nick Coghlan
Guest
Posts: n/a
 
      03-04-2005
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 | http://www.velocityreviews.com/forums/(E-Mail Removed) | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to call function whose function call with arguments is in astring Options grbgooglefan C Programming 4 01-30-2008 05:12 PM
How to call function whose function call with arguments is in astring grbgooglefan C++ 2 01-30-2008 07:18 AM
How to call function whose function call with arguments is in astring Options grbgooglefan C Programming 0 01-30-2008 04:19 AM
defined? for recursive function call v/s defined? for function call stack Alok Ruby 3 04-13-2006 11:53 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments