Python dynamic class names [duplicate]

送分小仙女□ 提交于 2019-12-14 03:43:17

问题


Possible Duplicate:
Dynamic loading of python modules
python: How to add property to a class dynamically?

I have a dictionary with the filename and class names how can I import this class names and how can I create this classes?

Example:

classNames = { 'MCTest':MCTestClass}

I want to import the MCTest and create the MCTestClass.


回答1:


You have to use the __import__ function:

http://docs.python.org/library/functions.html#import

Example from doc page:

>>> import sys
>>> name = 'foo.bar.baz'
>>> __import__(name)
<module 'foo' from ...>
>>> baz = sys.modules[name]
>>> baz
<module 'foo.bar.baz' from ...>

To instantiate a class from baz you should be able to do:

>>> SomeClass = getattr(baz, 'SomeClass')
>>> obj = SomeClass()



回答2:


From turbogears.util:

def load_class(dottedpath):
    """Load a class from a module in dotted-path notation.

    E.g.: load_class("package.module.class").

    Based on recipe 16.3 from Python Cookbook, 2ed., by Alex Martelli,
    Anna Martelli Ravenscroft, and David Ascher (O'Reilly Media, 2005)

    """
    assert dottedpath is not None, "dottedpath must not be None"
    splitted_path = dottedpath.split('.')
    modulename = '.'.join(splitted_path[:-1])
    classname = splitted_path[-1]
    try:
        try:
            module = __import__(modulename, globals(), locals(), [classname])
        except ValueError: # Py < 2.5
            if not modulename:
                module = __import__(__name__.split('.')[0],
                    globals(), locals(), [classname])
    except ImportError:
        # properly log the exception information and return None
        # to tell caller we did not succeed
        logging.exception('tg.utils: Could not import %s'
            ' because an exception occurred', dottedpath)
        return None
    try:
        return getattr(module, classname)
    except AttributeError:
        logging.exception('tg.utils: Could not import %s'
            ' because the class was not found', dottedpath)
        return None

use it like this:

cls = load_class('package.module.class')
obj = cls(...)


来源:https://stackoverflow.com/questions/7281110/python-dynamic-class-names

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!