instantiate a class with a variable

bruno at modulix onurb at xiludom.gro
Tue Mar 28 03:11:08 EST 2006


John wrote:
> Hi, is it possible to instantiate a class with a variable.
> 
> example
> 
> class foo:
>     def method(self):
>         pass
> 
> x='foo'
> 
> Can I use variable x value to create an instance of my class?

You got examples using string 'foo', now if all you need is to store or
pass around the class, just use it as any other object (yes, classes
*are* objects):

class Foo(object): pass

x = Foo

f = x()
assert type(f) is Foo


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list