make a class instance from a string ?

2006-02-23 Thread Bo Yang
Hi, I know in java , we can use class.ForName("classname") to get an instance of the class 'classname' from a string , in python , how do I do that ? Thanks in advance ! -- http://mail.python.org/mailman/listinfo/python-list

Re: make a class instance from a string ?

2006-02-23 Thread bearophileHUGS
Bo Yang: > to get an instance of the class 'classname' from a > string , in python , how do I do that ? This is a possibile way: class C: pass c = locals()["C"]() print c Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: make a class instance from a string ?

2006-02-23 Thread Diez B. Roggisch
Bo Yang wrote: > Hi, > I know in java , we can use > > class.ForName("classname") > > > to get an instance of the class 'classname' from a > string , in python , how do I do that ? You can use getattr(module, classname)(*arguments) Diez -- http://mail.python.org/mailman/listinfo/python

Re: make a class instance from a string ?

2006-02-23 Thread Luke Plant
Bo Yang wrote: > I know in java , we can use > > class.ForName("classname") > > > to get an instance of the class 'classname' from a > string , in python , how do I do that ? In Python, classes are first class objects, so normally you would pass the class itself around, rather than use the names

Re: make a class instance from a string ?

2006-02-23 Thread Diez B. Roggisch
Luke Plant wrote: > Bo Yang wrote: > >> I know in java , we can use >> >> class.ForName("classname") >> >> >> to get an instance of the class 'classname' from a >> string , in python , how do I do that ? > > In Python, classes are first class objects, so normally you would pass > the class itsel

Re: make a class instance from a string ?

2006-02-23 Thread Terry Hancock
On 23 Feb 2006 05:22:25 -0800 "Luke Plant" <[EMAIL PROTECTED]> wrote: > In Python, classes are first class objects, so normally > you would pass the class itself around, rather than use > the names of classes. Of course that might not be > practical or applicable in your situation. It is in fact,

Re: make a class instance from a string ?

2006-02-24 Thread Mike Woodhouse
Is there anything particularly bad with obj = eval(classname + "()") ? It appears to work, but I'm a noobie so I could be missing something nasty, in which any edication would be gratefully received. Mike -- http://mail.python.org/mailman/listinfo/python-list

Re: make a class instance from a string ?

2006-02-24 Thread Scott David Daniels
Mike Woodhouse wrote: > Is there anything particularly bad with > obj = eval(classname + "()") > It appears to work, but I'm a noobie so I could be missing something > nasty, in which any edication would be gratefully received. It is a little too indirect. Usually wanting to use "eval" or "

Re: make a class instance from a string ?

2006-02-24 Thread Steven D'Aprano
On Fri, 24 Feb 2006 06:37:27 -0800, Mike Woodhouse wrote: > Is there anything particularly bad with > > obj = eval(classname + "()") > > ? > > It appears to work, but I'm a noobie so I could be missing something > nasty, in which any edication would be gratefully received. In your own code, th

Re: make a class instance from a string ?

2006-02-26 Thread M�ta-MCI
Hi! Perso, I like this... MCI -- http://mail.python.org/mailman/listinfo/python-list