Newbie: Changing a string to a class attribute.

J

Jacob H

This is a very simple problem and I'm sure the answer is a no brainer.
However my brain can't see the answer. ;)

Given code like this:

def exec_method(object, method):
# object is an instantiated object, e.g. log
# method is a string that matches a method of object, e.g.
"update"
# code stuff here that calls object.method, e.g. log.update()

What is the best way to turn method, a string, into a valid reference
to the actual class method? My first thought was eval(). But I can't
do this:

eval("class.method()")

Eval will look for a method actually called method() and there isn't
one. What's a good solution for this? Heck, for all I know, Python
implicitly provides functionality to solve this problem. Can anyone
help? :)

Jake
 
D

David C. Fox

Jacob said:
This is a very simple problem and I'm sure the answer is a no brainer.
However my brain can't see the answer. ;)

Given code like this:

def exec_method(object, method):
# object is an instantiated object, e.g. log
# method is a string that matches a method of object, e.g.
"update"
# code stuff here that calls object.method, e.g. log.update()

What is the best way to turn method, a string, into a valid reference
to the actual class method? My first thought was eval(). But I can't
do this:

eval("class.method()")

Eval will look for a method actually called method() and there isn't
one. What's a good solution for this? Heck, for all I know, Python
implicitly provides functionality to solve this problem. Can anyone
help? :)

getattr(x, method)()

However, it isn't usually necessary to do this, because if x is an
object with method update, x.update is an object which can be passed or
stored.

David
 
P

Peter Abel

This is a very simple problem and I'm sure the answer is a no brainer.
However my brain can't see the answer. ;)

Given code like this:

def exec_method(object, method):
# object is an instantiated object, e.g. log
# method is a string that matches a method of object, e.g.
"update"
# code stuff here that calls object.method, e.g. log.update()

What is the best way to turn method, a string, into a valid reference
to the actual class method? My first thought was eval(). But I can't
do this:

eval("class.method()")

You're quite close!
Eval will look for a method actually called method() and there isn't
one. What's a good solution for this? Heck, for all I know, Python
implicitly provides functionality to solve this problem. Can anyone
help? :)

Jake
.... def up_date(self):
.... print 'update'
.... .... eval('object.'+method) ()
....
Hope that helps.
Regards
Peter
 
P

Peter Abel

Jeff Epler said:
Why not write
def exec_method(object, method):
getattr(object, method)()
no nasty 'eval' there, thank goodness.

Jeff

It's okay.
Seems to be clearer.

Peter
 
J

Jacob H

Thanks everybody.

getattr(object, method)()

This solution works just fine for me. Incidentally, the reason I can't
just write object.method() is that I'm attempting to implement the
Command design pattern, like so:

class Command:
def execute(self):
pass

class ConcreteCommand(Command)
def __init__(self, receiver, action):
self.receiver = receiver
self.action = action

def execute(self):
if hasattr(self.receiver, self.action):
getattr(self.receiver, self.action)()

As can be seen, I needed a way to dynamically execute a given object
method. That's why I had to ask this question in the first place.

Thanks for the help! :)

Jake
 

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

Forum statistics

Threads
474,266
Messages
2,571,342
Members
48,018
Latest member
DelilahDen

Latest Threads

Top