instantiate a class with a variable

John johnwadeunderwood at yahoo.com
Tue Mar 28 23:40:31 CEST 2006


Thanks for the help, this was exactly what I needed. Working Great
bruno at modulix wrote:
> 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