468,549 Members | 2,199 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,549 developers. It's quick & easy.

make a class instance from a string ?

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 !

Feb 23 '06 #1
9 26741
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

Feb 23 '06 #2
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
Feb 23 '06 #3
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 of classes. Of
course that might not be practical or applicable in your situation.

Luke

Feb 23 '06 #4
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 itself around, rather than use the names of classes. Of
course that might not be practical or applicable in your situation.


While JAVA is severely limited regarding the number of seats in the first
class, classes _are_ sitting there.

The need for dynamic attribute look up is even more frequent in python -
think getattr, __getitem__, __getattr__.

Diez
Feb 23 '06 #5
On 23 Feb 2006 05:22:25 -0800
"Luke Plant" <lu********@gmail.com> 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, a particular source of annoyance when the
object is meant to be serialized to disk with pickle or
the like, and especially when it is an extension object.

A good idiom for "look me up in the source code after you
unpack me" is required. I think some things like ZODB
will already do that for you, but it seems basic enough
that there ought to be a general approved method of doing
that in Python -- you know, "one obvious way".

--
Terry Hancock (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com
Feb 23 '06 #6
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

Feb 24 '06 #7
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 "exec"
means your code is probably not properly structured (a "code smell").
You can pass classes around as values; you typically needn't work with
their names. If the name comes from outside, a dictionary of names to
classes means you can re-implement your code without being tightly
coupled to your I/O formats. If you still want to use the name
I'd go with:
globals()[classname]()
over eval, but it is your code.

Here's a danger to think about:
Suppose your source of class names has:
'__import__(os).system("delete critical.file")'
for a class name?
--Scott David Daniels
sc***********@acm.org
Feb 24 '06 #8
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, that you control? Nothing particularly bad.

In your public web application, using classname supplied by some anonymous
remote user? It could be bad:

obj = eval("(lambda : os.system('ls'))" + "()")

only, instead of 'ls', imagine a more... serious shell command.

Using eval is like running a small piece of Python code. If you control
the code (or to be precise, the expression) then it is no more dangerous
than any other code you choose to run.

On the other hand, if you give access to your system to anonymous users,
you have to assume some of them will be malicious, and they will be a lot
more inventive searching for security holes than you.
--
Steven.

Feb 24 '06 #9
Hi!

Perso, I like this...

MCI
Feb 27 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Göran Tänzer | last post: by
5 posts views Thread by Cyril Gupta | last post: by
4 posts views Thread by Bernd Muent | last post: by
4 posts views Thread by Pyenos | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.