Call a classmethod on a variable class name
Matthew Keene
dfg778 at yahoo.com.au
Sun Apr 13 05:09:42 EDT 2008
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 !
More information about the Python-list
mailing list