dynamic invoke

L

lukasz.f24

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.
 
J

Jarek Zgoda

(e-mail address removed) 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()
 
L

lukasz.f24

(e-mail address removed) napisa³(a):



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

--
Jarek Zgoda
Skype: jzgoda | GTalk: (e-mail address removed) | 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.
 
D

Diez B. Roggisch

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
 
S

Sushant

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()
 
D

Diez B. Roggisch

Sushant said:
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
 
S

Steven D'Aprano

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
 
S

Sushant

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.
 
J

Jarek Zgoda

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top