469,592 Members | 2,026 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

Calling class method by name passed in variable

Greetings,

Can someone suggest an efficient way of calling method whose name is
passed in a variable?

Given something like:

class X:
#...
def a(self):
# ...

def b(self):
# ...

#...

x = X()
#...
v = 'a'

How do I call the method of x whose name is stored in v?

PHP code for this would be:

class X {
function a() {
}
}

$x = new X();
$v = 'a';
$x->$v();

I need a solution for Python. Could you suggest anything?

The task it to call a function whose name is taken from user-supplied
input.

Thanks.
Jun 27 '08 #1
6 2747
Sagari wrote:
Greetings,

Can someone suggest an efficient way of calling method whose name is
passed in a variable?

Given something like:

class X:
#...
def a(self):
# ...

def b(self):
# ...

#...

x = X()
#...
v = 'a'

How do I call the method of x whose name is stored in v?
Use getattr (stands for get attribute) to do this.

fn = getattr(x, v) # Get the method named by v
fn(...) # Call it

Or in one line:

getattr(x,v)(...)
Gary Herron

PHP code for this would be:

class X {
function a() {
}
}

$x = new X();
$v = 'a';
$x->$v();

I need a solution for Python. Could you suggest anything?

The task it to call a function whose name is taken from user-supplied
input.

Thanks.
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #2
Sagari a écrit :
Greetings,

Can someone suggest an efficient way of calling method whose name is
passed in a variable?
method = getattr(obj, 'method_name', None)
if callable(method):
method(args)
Jun 27 '08 #3
Bruno Desthuilliers <br********************@websiteburo.invalidwrote :
Can someone suggest an efficient way of calling method whose name is
passed in a variable?

method = getattr(obj, 'method_name', None)
if callable(method):
method(args)
I think that that is needless LBYL...

getattr(obj, 'method_name')(args)

Will produce some perfectly good exceptions
>>class A(object):
... def __init__(self):
... self.x = 2
... def f(self, x):
... print x == self.x
...
>>obj = A()
getattr(obj, 'floop')(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'floop'
>>getattr(obj, 'x')(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>getattr(obj, 'f')(1)
False
>>>
--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 27 '08 #4
Nick Craig-Wood a écrit :
Bruno Desthuilliers <br********************@websiteburo.invalidwrote :
>>Can someone suggest an efficient way of calling method whose name is
passed in a variable?
method = getattr(obj, 'method_name', None)
if callable(method):
method(args)

I think that that is needless LBYL...
From experience, it isn't.

getattr(obj, 'method_name')(args)

Will produce some perfectly good exceptions
The problem is that you can't tell (without reading the exception's
message and traceback etc) if the exception happened in you code or
within the called method.

Jun 27 '08 #5
Bruno Desthuilliers <br********************@websiteburo.invalidwrote :
Nick Craig-Wood a ?crit :
Bruno Desthuilliers <br********************@websiteburo.invalidwrote :
>Can someone suggest an efficient way of calling method whose name is
passed in a variable?

method = getattr(obj, 'method_name', None)
if callable(method):
method(args)
I think that that is needless LBYL...

From experience, it isn't.
getattr(obj, 'method_name')(args)

Will produce some perfectly good exceptions

The problem is that you can't tell (without reading the exception's
message and traceback etc) if the exception happened in you code or
within the called method.
I guess it depends on whether you are expecting it to ever fail or
not. If a user types in method_name then yes you are right checking
stuff and producing a nice error is good. However if it is part of
your internal program flow and you never expect it to go wrong in
normal operation then I'd be quite happy with the exception and
backtrace to debug the problem.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 27 '08 #6
Thanks to everyone for the advice. In fact, since all the called
methods trap necessary exceptions, I see no big problems with that.

With all respect,

Konstantin
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by JS | last post: by
2 posts views Thread by Stan | last post: by
6 posts views Thread by Ray Schumacher | last post: by
8 posts views Thread by Jackson | last post: by
20 posts views Thread by tshad | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.