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:
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 funct
Once again: thank you.
--
http://mail.python.org/mailman/listinfo/python-list
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)
-
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." + function
> Thank you both for your quick answers.
Thank you *all* for your quick answers.
--
http://mail.python.org/mailman/listinfo/python-list
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:
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 w
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 Hof
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:/
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()
foobar
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
12 matches
Mail list logo