Creating class instance from module and class name

G

gentlestone

Suppose I've saved the class name and (don't know how) I've also saved
the Class's module (package path or I don't know what's the name for
XYZ "from X.Y.Z import ...). How can I construct a new class according
to saved informations? If I don't know what Class it could be, only I
have the saved Class name?
 
S

Steven D'Aprano

Suppose I've saved the class name and (don't know how) I've also saved
the Class's module (package path or I don't know what's the name for XYZ
"from X.Y.Z import ...). How can I construct a new class according to
saved informations? If I don't know what Class it could be, only I have
the saved Class name?


If you have the module *object*, and the name of the class, then you can
do this:

theclass = getattr(module, "MyClass")

to get the class itself, and then call it as normal to instantiate it:

instance = theclass(args)

Classes are just like any other object in that regard.

If you have the *name* of the module, you can import it first to get the
module object:

module = __import__('module_name')
theclass = getattr(module, "MyClass")
instance = theclass(args)


There may be some complications if you have a dotted package name, in
which case the docs for __import__ are your friend :)
 
G

gentlestone

If you have the module *object*, and the name of the class, then you can
do this:

theclass = getattr(module, "MyClass")

to get the class itself, and then call it as normal to instantiate it:

instance = theclass(args)

Classes are just like any other object in that regard.

If you have the *name* of the module, you can import it first to get the
module object:

module = __import__('module_name')
theclass = getattr(module, "MyClass")
instance = theclass(args)

There may be some complications if you have a dotted package name, in
which case the docs for __import__ are your friend :)

thx for help

one more question - __class__ is the way for getting the classname
from the class instance -
how can I get the module name?
 
C

Chris Rebert

thx for help

one more question - __class__ is the way for getting the classname
from the class instance -

No, that'd be some_instance.__class__.__name__

Cheers,
Chris
 
S

Steven D'Aprano

one more question - __class__ is the way for getting the classname from
the class instance -

Not quite. __class__ returns the class object, not the name. Given the
class object, you ask for __name__ to get the name it was defined as[1].
For example:
.... pass
....
'MyClass'


how can I get the module name?
'math'

But if possible, you should pass around the actual module and class
objects rather than just their names. The only time you need to use the
*names* rather than the actual objects themselves is if you are getting
the information from a source outside of Python, e.g. user input, or a
text file, etc.

E.g. suppose you have imported the decimal module, and you want access to
the Decimal class elsewhere. You can pass the names "decimal" and
"Decimal" to some function, and use __import__() and getattr() to access
them. Or you can do this:

.... print "Module is", mod
.... print "Class is", cls
.... return cls(0) # or whatever you want to do with it...
....Module is <module 'math' from '/usr/lib/python2.5/lib-dynload/
mathmodule.so'>
Class is <class 'decimal.Decimal'>
Decimal("0")









[1] But not necessarily the name it has now.
 
D

Diez B. Roggisch

Steven said:
If you have the module *object*, and the name of the class, then you can
do this:

theclass = getattr(module, "MyClass")

to get the class itself, and then call it as normal to instantiate it:

instance = theclass(args)

Classes are just like any other object in that regard.

If you have the *name* of the module, you can import it first to get the
module object:

module = __import__('module_name')

Not working for modules in packages, it will return the toplevel module
only. I never remember if there is a convenience-function, what I
usually do is this:

module = __import__(module_name)
for part in module_name.split(".")[1:]:
module = getattr(module, part)



Diez
 
B

Bruno Desthuilliers

gentlestone a écrit :

(snip)
one more question - __class__ is the way for getting the classname
from the class instance -

obj.__class__ is a reference to the class object itself, not it's name.
how can I get the module name?

module_obj.__name__

And while we're at it: obj.__class__.__module__ might be of some
interest too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top