[Beginner] Calling a function by its name in a string

2005-07-27 Thread Tito
Hi all:

Is there a metalanguage capability in Python (I know there are many) to 
call a function having its name in a string?

Something like:
__call__(foo)

instead of:
foo()

Regards,
Tito
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Beginner] Calling a function by its name in a string

2005-07-27 Thread Bill Mill
On 7/27/05, Tito [EMAIL PROTECTED] wrote:
 Hi all:
 
 Is there a metalanguage capability in Python (I know there are many) to
 call a function having its name in a string?
 
 Something like:
 __call__(foo)
 
 instead of:
 foo()
 

 def foo(): print foobarred
...
 foo()
foobarred
 eval(foo())
foobarred


Peace
Bill Mill
bill.mill at gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Beginner] Calling a function by its name in a string

2005-07-27 Thread Paolino
Tito wrote:
 Hi all:
 
 Is there a metalanguage capability in Python (I know there are many) to 
 call a function having its name in a string?
 
 Something like:
 __call__(foo)
 
 instead of:
 foo()
 
 Regards,
 Tito
eval('foo()') should do, but it's said a bad practice ;)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Beginner] Calling a function by its name in a string

2005-07-27 Thread Michael Hoffman
Tito wrote:
 Hi all:
 
 Is there a metalanguage capability in Python (I know there are many) to 
 call a function having its name in a string?
 
 Something like:
 __call__(foo)
 
 instead of:
 foo()

locals()[foo]() will be a little more predictable than eval(foo()).
-- 
Michael Hoffman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Beginner] Calling a function by its name in a string

2005-07-27 Thread Grant Edwards
On 2005-07-27, Paolino [EMAIL PROTECTED] wrote:

 Is there a metalanguage capability in Python (I know there are many) to 
 call a function having its name in a string?

 eval('foo()') should do, but it's said a bad practice ;)

An alternative to eval() is:

 def foo():
...   print foo was called
... 
 s = foo
 globals()[s]()
foo was called
 

-- 
Grant Edwards   grante Yow!  I'm meditating on
  at   the FORMALDEHYDE and the
   visi.comASBESTOS leaking into my
   PERSONAL SPACE!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Beginner] Calling a function by its name in a string

2005-07-27 Thread Tito
Thank you both for your quick answers.

What I wanted is to parameterize a function with another member 
function, like this:

def printFunctionForEach(collection, functionName):
   for elem in collection:
 print eval(elem. + functionName + ())

Moreover, I wanted to do it with a property:

def printPropertyForEach(collection, propertyName):
   for elem in collection:
 print eval(elem. + propertyName)

Is there another approach to do it?

Regards,
Tito
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Beginner] Calling a function by its name in a string

2005-07-27 Thread Tito
 Thank you both for your quick answers.

Thank you *all* for your quick answers.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Beginner] Calling a function by its name in a string

2005-07-27 Thread Bill Mill
On 7/27/05, Tito [EMAIL PROTECTED] wrote:
 Thank you both for your quick answers.
 
 What I wanted is to parameterize a function with another member
 function, like this:
 
 def printFunctionForEach(collection, functionName):
for elem in collection:
  print eval(elem. + functionName + ())
 
 Moreover, I wanted to do it with a property:
 
 def printPropertyForEach(collection, propertyName):
for elem in collection:
  print eval(elem. + propertyName)
 
 Is there another approach to do it?
 

Sure, piece of cake:

 class test:
... def func1(self): print 'func1 called'
...
 class test2:
... def func1(self): print 'other func1'
...
 x = [test(), test2(), test()]
 def call_this_func(lst, func_name):
... for e in lst:
... getattr(e, func_name)()
...
 call_this_func(x, 'func1')
func1 called
other func1
func1 called


Note that the getattr raises an AttributeError if func_name doesn't
exist in the object; you should probably wrap it in a try/except.

Peace
Bill Mill
bill.mill at gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Beginner] Calling a function by its name in a string

2005-07-27 Thread Paul Rubin
Tito [EMAIL PROTECTED] writes:
 def printPropertyForEach(collection, propertyName):
for elem in collection:
  print eval(elem. + propertyName)
 
 Is there another approach to do it?

Yes, use the getattr function:

   for elem in collection:
 print getattr(elem, propertyName)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Beginner] Calling a function by its name in a string

2005-07-27 Thread Tito
Once again: thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Beginner] Calling a function by its name in a string

2005-07-27 Thread Peter Hansen
Tito wrote:
 Thank you both for your quick answers.
 
 What I wanted is to parameterize a function with another member 
 function, like this:
 
 def printFunctionForEach(collection, functionName):
   for elem in collection:
 print eval(elem. + functionName + ())

Note: member function is spelled method in Python.

 Moreover, I wanted to do it with a property:
 
 def printPropertyForEach(collection, propertyName):
   for elem in collection:
 print eval(elem. + propertyName)

And property (the way you are using it) is spelled attribute.

In Python, properties are something else, similar to but more than just 
attributes.

Use of such terms according to conventional Python usage will in future 
make it somewhat easier to be understood and for you to understand the 
responses.

Cheers,
-Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Beginner] Calling a function by its name in a string

2005-07-27 Thread Steven D'Aprano
On Wed, 27 Jul 2005 14:18:25 -0400, Bill Mill wrote:

 On 7/27/05, Tito [EMAIL PROTECTED] wrote:
 Hi all:
 
 Is there a metalanguage capability in Python (I know there are many) to
 call a function having its name in a string?
 
 Something like:
 __call__(foo)
 
 instead of:
 foo()
 
 
 def foo(): print foobarred
 ...
 foo()
 foobarred
 eval(foo())
 foobarred

Which is dangerous beyond belief if you are getting your string foo()
from a user, and if you aren't, you almost certainly can refactor your
code so you don't need eval.

You know, I really am getting sick of (1) people who ask how to shoot
themselves in the foot and (2) people who cheerfully load the gun and hand
it to them without a word of warning about the consequences. And then
we all act surprised when we learn about the latest virus or security hole
that allows a hostile user to use a music player or paint program to take
over the entire operating system. Or whatever.

We're all adults here only works for people who ARE adults. If you have
to ask about eval, you can't be trusted with it without at least a warning.


-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list