![]() |
Creating class instance from module and class name
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? |
Re: Creating class instance from module and class name
On Mon, 05 Oct 2009 23:32:27 -0700, gentlestone wrote:
> 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 :) -- Steven |
Re: Creating class instance from module and class name
On 6. Okt, 08:55 h., Steven D'Aprano
<ste...@REMOVE.THIS.cybersource.com.au> wrote: > On Mon, 05 Oct 2009 23:32:27 -0700, gentlestone wrote: > > 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 :) > > -- > Steven 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? |
Re: Creating class instance from module and class name
On Tue, Oct 6, 2009 at 12:09 AM, gentlestone <tibor.beck@hotmail.com> wrote:
> On 6. Okt, 08:55 h., Steven D'Aprano > <ste...@REMOVE.THIS.cybersource.com.au> wrote: >> On Mon, 05 Oct 2009 23:32:27 -0700, gentlestone wrote: >> > 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 :) >> >> -- >> Steven > > 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 -- http://blog.rebertia.com |
Re: Creating class instance from module and class name
On Tue, 06 Oct 2009 00:09:08 -0700, gentlestone wrote:
> 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: >>> class MyClass: .... pass .... >>> instance = MyClass() >>> instance.__class__ <class __main__.MyClass at 0xb7f4465c> >>> MyClass.__name__ 'MyClass' >>> instance.__class__.__name__ 'MyClass' > how can I get the module name? >>> import math >>> math.__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: >>> def example(mod, cls): .... print "Module is", mod .... print "Class is", cls .... return cls(0) # or whatever you want to do with it... .... >>> >>> example(math, decimal.Decimal) # pass the objects, not the names 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. -- Steven |
Re: Creating class instance from module and class name
Steven D'Aprano schrieb:
> On Mon, 05 Oct 2009 23:32:27 -0700, gentlestone wrote: > >> 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') 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 |
Re: Creating class instance from module and class name
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. |
| All times are GMT. The time now is 04:36 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.