471,337 Members | 1,141 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

dynamic invoke

Hello,

Is there any way (other then eval) to invoke a method by passing
method name in a string.
It's very simple in php:
$oFoo = new Foo();
$dynamiMethod = "bar";
$oFoo->$dynamiMethod();

Unfortunately I can't find a good solution to do the same thing in
python. Does it have some build-in function to do it?

Kind Regards,

Lukasz.

Oct 19 '07 #1
11 1365
lu********@gmail.com napisa³(a):
Is there any way (other then eval) to invoke a method by passing
method name in a string.
It's very simple in php:
$oFoo = new Foo();
$dynamiMethod = "bar";
$oFoo->$dynamiMethod();

Unfortunately I can't find a good solution to do the same thing in
python. Does it have some build-in function to do it?
foo = getattr(module_or_object, 'function_name')
foo()

--
Jarek Zgoda
Skype: jzgoda | GTalk: zg***@jabber.aster.pl | voice: +48228430101

"We read Knuth so you don't have to." (Tim Peters)
Oct 19 '07 #2
On 19 Oct, 11:45, Jarek Zgoda <jzg...@o2.usun.plwrote:
lukasz....@gmail.com napisa³(a):
Is there any way (other then eval) to invoke a method by passing
method name in a string.
It's very simple in php:
$oFoo = new Foo();
$dynamiMethod = "bar";
$oFoo->$dynamiMethod();
Unfortunately I can't find a good solution to do the same thing in
python. Does it have some build-in function to do it?

foo = getattr(module_or_object, 'function_name')
foo()

--
Jarek Zgoda
Skype: jzgoda | GTalk: zg...@jabber.aster.pl | voice: +48228430101

"We read Knuth so you don't have to." (Tim Peters)
Superb!
I was lookig for something like this. Belive me or not but i spent
lots of time looking for this simple solution :)

Cheers.

Oct 19 '07 #3
lu********@gmail.com wrote:
On 19 Oct, 11:45, Jarek Zgoda <jzg...@o2.usun.plwrote:
>lukasz....@gmail.com napisa³(a):
Is there any way (other then eval) to invoke a method by passing
method name in a string.
It's very simple in php:
$oFoo = new Foo();
$dynamiMethod = "bar";
$oFoo->$dynamiMethod();
Unfortunately I can't find a good solution to do the same thing in
python. Does it have some build-in function to do it?

foo = getattr(module_or_object, 'function_name')
foo()

--
Jarek Zgoda
Skype: jzgoda | GTalk: zg...@jabber.aster.pl | voice: +48228430101

"We read Knuth so you don't have to." (Tim Peters)

Superb!
I was lookig for something like this. Belive me or not but i spent
lots of time looking for this simple solution :)
The above clearly is a solution to your problem. I just wonder if you _need_
it. PHP doesn't have the concept of a function reference. So you need to
store/use names.

But in Python you can do this:

def foo():
print "foo"

callback = foo
callback()

As trivial this code is, it illustrates one thing: passing around functions
is as easy as passing around other values. So maybe you don't need a
function name. Only a suggestion to ponder about though, it might be that
your usecase is not suited for the above.

Diez
Oct 19 '07 #4
On Oct 19, 12:39 pm, lukasz....@gmail.com wrote:
Hello,

Is there any way (other then eval) to invoke a method by passing
method name in a string.
It's very simple in php:
$oFoo = new Foo();
$dynamiMethod = "bar";
$oFoo->$dynamiMethod();

Unfortunately I can't find a good solution to do the same thing in
python. Does it have some build-in function to do it?

Kind Regards,

Lukasz.
Use apply(): http://docs.python.org/lib/non-essen...-in-funcs.html

Nils

Oct 19 '07 #5
Nils wrote:
On Oct 19, 12:39 pm, lukasz....@gmail.com wrote:
>Hello,

Is there any way (other then eval) to invoke a method by passing
method name in a string.
It's very simple in php:
$oFoo = new Foo();
$dynamiMethod = "bar";
$oFoo->$dynamiMethod();

Unfortunately I can't find a good solution to do the same thing in
python. Does it have some build-in function to do it?

Kind Regards,

Lukasz.

Use apply(): http://docs.python.org/lib/non-essen...-in-funcs.html
Apply doesn't help the OP - it's useful if you already have a function
pointer.

And it's obsoleted by the * and ** parameter passing mechanism.

Diez
Oct 19 '07 #6
Python will not allow string to be used a function pointer. It is type unsafe.
Best way is to convert string into function pointers manually.

if dynamicMethod == 'bar':
method = oFoo->bar
else:
method = oFoo->default
method()

On Friday 19 October 2007 7:56 am, Diez B. Roggisch wrote:
Nils wrote:
On Oct 19, 12:39 pm, lukasz....@gmail.com wrote:
Hello,

Is there any way (other then eval) to invoke a method by passing
method name in a string.
It's very simple in php:
$oFoo = new Foo();
$dynamiMethod = "bar";
$oFoo->$dynamiMethod();

Unfortunately I can't find a good solution to do the same thing in
python. Does it have some build-in function to do it?

Kind Regards,

Lukasz.
Use apply():
http://docs.python.org/lib/non-essen...-in-funcs.html

Apply doesn't help the OP - it's useful if you already have a function
pointer.

And it's obsoleted by the * and ** parameter passing mechanism.

Diez
Oct 19 '07 #7
Sushant wrote:
Python will not allow string to be used a function pointer. It is type
unsafe. Best way is to convert string into function pointers manually.

if dynamicMethod == 'bar':
method = oFoo->bar
else:
method = oFoo->default
method()
Sorry to say so, but that answer is bogus. It's not even Python!

Jarek has already shown how to solve this problem. And type-safety-issues
have nothing to do with it at all.

Diez
Oct 19 '07 #8
On Fri, 19 Oct 2007 11:34:39 +0000, Nils wrote:
Use apply():
http://docs.python.org/lib/non-essen...-in-funcs.html
No, don't use apply. Not only does it not solve the original poster's
problem, it is a deprecated function. You shouldn't use it in new code.

--
Steven.
Oct 19 '07 #9
On Fri, 19 Oct 2007 10:29:12 -0400, Sushant wrote:
Python will not allow string to be used a function pointer. It is type
unsafe. Best way is to convert string into function pointers manually.

if dynamicMethod == 'bar':
method = oFoo->bar
else:
method = oFoo->default
method()

I don't know what language you're programming in there, but it isn't
Python.

method = oFoo->bar
^
SyntaxError: invalid syntax

--
Steven.
Oct 19 '07 #10
I did not know about getattr and it is the right thing.

getattr seems to be converting string into function pointer and I am just
saying that string cannot be used as a function pointer in Python as may be
in PHP.

I copied the PHP code so I did not replace arrow with dot. Good point :)

-Sushant.

On Friday 19 October 2007 11:05 am, Diez B. Roggisch wrote:
Sushant wrote:
Python will not allow string to be used a function pointer. It is type
unsafe. Best way is to convert string into function pointers manually.

if dynamicMethod == 'bar':
method = oFoo->bar
else:
method = oFoo->default
method()

Sorry to say so, but that answer is bogus. It's not even Python!

Jarek has already shown how to solve this problem. And type-safety-issues
have nothing to do with it at all.

Diez
Oct 19 '07 #11
Sushant napisa³(a):
getattr seems to be converting string into function pointer and I am just
saying that string cannot be used as a function pointer in Python as may be
in PHP.
It seems, but it does not. Getattr performs lookup on object's
attributes dict, it does not "convert" anything. The abstraction of
"function pointer" is also wrong here, it's a reference to an object
(any object, not just function). The result might seems similar, but
works quite differently.

--
Jarek Zgoda
Skype: jzgoda | GTalk: zg***@jabber.aster.pl | voice: +48228430101

"We read Knuth so you don't have to." (Tim Peters)
Oct 22 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by ogled | last post: by
3 posts views Thread by Stephen Gennard | last post: by
4 posts views Thread by Tamir Khason | last post: by
10 posts views Thread by Yechezkal Gutfreund | last post: by
2 posts views Thread by Luis Arvayo | last post: by
reply views Thread by pjr | last post: by
1 post views Thread by alexis.meilland | last post: by
9 posts views Thread by =?Utf-8?B?RXZlcnQ=?= | last post: by
26 posts views Thread by Aaron \Castironpi\ Brady | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.