Re: [Beginner] Calling a function by its name in a string
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

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

From: Peter Hansen <peter@engcorp.com>
Date: Thu Jul 28 2005 - 00:22:45 CEST

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
Received on Thu Sep 29 17:12:09 2005