473,385 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2365
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: bonono | last post by:
Hi, Suppose my class definition is like this : class A: name = "A" @classmethod def foo(cls): cls.__super.foo()
6
by: Jeff Frederick | last post by:
Hello all, I am interested in using the value from a field to call a function. For example I have a table and one of the field names is "Function." So for each record I input the name of the...
3
by: andychambers2002 | last post by:
Hi, I'm trying to write a method that needs to know both the class name and the instance details class A: @classmethod def meth(cls, self): print cls
1
by: Faheem Mitha | last post by:
Hi, Consider the following small script. My understanding of how this works is that, conceptually, class B holds a separate copy of variable x from class A. Nearly everything behaves the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.