By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,244 Members | 1,972 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,244 IT Pros & Developers. It's quick & easy.

Calling class method by name passed in variable

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.