Creating class instance from module and class name

Discussion in 'Python' started by gentlestone, Oct 6, 2009.

  1. gentlestone

    gentlestone Guest

    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?
    gentlestone, Oct 6, 2009
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Oct 6, 2009
    #2
    1. Advertising

  3. gentlestone

    gentlestone Guest

    On 6. Okt, 08:55 h., Steven D'Aprano
    <> 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?
    gentlestone, Oct 6, 2009
    #3
  4. gentlestone

    Chris Rebert Guest

    On Tue, Oct 6, 2009 at 12:09 AM, gentlestone <> wrote:
    > On 6. Okt, 08:55 h., Steven D'Aprano
    > <> 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
    Chris Rebert, Oct 6, 2009
    #4
  5. 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
    Steven D'Aprano, Oct 6, 2009
    #5
  6. 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
    Diez B. Roggisch, Oct 6, 2009
    #6
  7. 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.
    Bruno Desthuilliers, Oct 6, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sridhar R
    Replies:
    14
    Views:
    1,398
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  2. Sebastien Binet
    Replies:
    4
    Views:
    871
    Sebastien Binet
    Dec 16, 2009
  3. Martin P. Hellwig
    Replies:
    1
    Views:
    374
    Martin P. Hellwig
    Mar 26, 2010
  4. gga
    Replies:
    3
    Views:
    154
    William Morgan
    Feb 19, 2005
  5. Olive
    Replies:
    4
    Views:
    241
    Olive
    Jul 6, 2012
Loading...

Share This Page