469,916 Members | 2,432 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Call a classmethod on a variable class name

I would like to be able to call a specific classmethod on a class name
that is going to be passed from another parameter. In other words, I
have a call that looks something like:

x = Foo.bar()

and I would like to generalise this so that I can make this call on any
particular class which provides the bar classmethod.

I have implemented this using exec, like so:

className = parameters.className
exec "x = " + className + ".bar()"

but this feels somewhat clumsy. (I do have the requisite exception
handling to cope with the supplied class not existing or not
implementing the bar method, by the way).

Is there any more Pythonesque way of doing this ? I guess what I'm
probably looking for is something like the way I understand the send
function works in Ruby
Jun 27 '08 #1
3 2226
On Apr 13, 9:51*am, Matthew Keene <dfg...@yahoo.com.auwrote:
I would like to be able to call a specific classmethod on a class name
that is going to be passed from another parameter. *In other words, I
have a call that looks something like:

* *x = Foo.bar()

and I would like to generalise this so that I can make this call on any
particular class which provides the bar classmethod.

I have implemented this using exec, like so:

* className = parameters.className
* exec "x = " + className + ".bar()"

but this feels somewhat clumsy. *(I do have the requisite exception
handling to cope with the supplied class not existing or not
implementing the bar method, by the way).

Is there any more Pythonesque way of doing this ? *I guess what I'm
probably looking for is something like the way I understand the send
function works in Ruby
If your class lives in the current global namespace, you can get it
with
>>cls = globals()[classname]
Then you can access its .bar() method directly:
>>cls.bar()
Example:
>>class Foo(object):
... @classmethod
... def bar(cls): print 'Oh my Baz!'
...
>>globals()['Foo']
<class '__main__.Foo'>
>>globals()['Foo'].bar()
Oh my Baz!

If your class lives in a module, just do getattr(module, classname)
instead to get the class object.

HTH

--
Arnaud

Jun 27 '08 #2
Arnaud Delobelle wrote:
>
If your class lives in the current global namespace, you can get it
with
>>cls = globals()[classname]

Then you can access its .bar() method directly:
>>cls.bar()

Example:
>class Foo(object):
... @classmethod
... def bar(cls): print 'Oh my Baz!'
...
>globals()['Foo']
<class ' main .Foo'>
>globals()['Foo'].bar()
Oh my Baz!

If your class lives in a module, just do getattr(module, classname)
instead to get the class object.

HTH

Yes, that feels much nicer and works well.

Thanks for the prompt reply !
Jun 27 '08 #3
Matthew Keene wrote:
I would like to be able to call a specific classmethod on a class name
that is going to be passed from another parameter. In other words, I
have a call that looks something like:

x = Foo.bar()

and I would like to generalise this so that I can make this call on any
particular class which provides the bar classmethod.

I have implemented this using exec, like so:

className = parameters.className
exec "x = " + className + ".bar()"
Yuck. No. Don't do that. As a general rule, anything you could build
and put into an exec is a functionality that is exposed more directly by
Python.
but this feels somewhat clumsy. (I do have the requisite exception
handling to cope with the supplied class not existing or not
implementing the bar method, by the way).

Is there any more Pythonesque way of doing this ? I guess what I'm
probably looking for is something like the way I understand the send
function works in Ruby
First off, if possible, don't pass the class name, but instead pass the
class itself:

class SomeClass:
def foo():
... whatever...

...
parameters.theClass = SomeClass
...
parameters.theClass.bar()

If you can't do that, then look up that class from the class name and
make your call:

class SomeClass:
def foo():
... whatever...

...
parameters.className = 'SomeClass'
...
theClass = globals()[parameters.className]
parameters.theClass.bar()
(Hint: It matters not whether foo is a classmethod, saticmathod or
normal method.)

Gary Herron



Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by andychambers2002 | last post: by
1 post views Thread by Faheem Mitha | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.