给定一个字符串作为Python函数的用户输入,如果当前定义的命名空间中有一个具有该名称的类,则希望从中获得一个类对象。本质上,我想实现一个函数,它将产生这样的结果:
class Foo:
pass
str_to_class("Foo")
==> <class __main__.Foo at 0x69ba0>
这有可能吗?
给定一个字符串作为Python函数的用户输入,如果当前定义的命名空间中有一个具有该名称的类,则希望从中获得一个类对象。本质上,我想实现一个函数,它将产生这样的结果:
class Foo:
pass
str_to_class("Foo")
==> <class __main__.Foo at 0x69ba0>
这有可能吗?
当前回答
是的,你可以做到。假设你的类存在于全局命名空间中,如下所示:
import types
class Foo:
pass
def str_to_class(s):
if s in globals() and isinstance(globals()[s], types.ClassType):
return globals()[s]
return None
str_to_class('Foo')
==> <class __main__.Foo at 0x340808cc>
其他回答
使用importlib最适合我。
import importlib
importlib.import_module('accounting.views')
它使用字符串点表示法来表示你想导入的python模块。
在任意代码执行方面,或者不希望用户传递的名称,您可以有一个可接受的函数/类名称列表,如果输入与列表中的一个匹配,则对其进行计算。
PS:我知道....有点晚了…但这是为将来偶然发现这个的人准备的。
你可以这样做:
globals()[class_name]
这是可行的:
import sys
def str_to_class(classname):
return getattr(sys.modules[__name__], classname)
我看了django是怎么处理的
django.utils。Module_loading有这个
def import_string(dotted_path):
"""
Import a dotted module path and return the attribute/class designated by the
last name in the path. Raise ImportError if the import failed.
"""
try:
module_path, class_name = dotted_path.rsplit('.', 1)
except ValueError:
msg = "%s doesn't look like a module path" % dotted_path
six.reraise(ImportError, ImportError(msg), sys.exc_info()[2])
module = import_module(module_path)
try:
return getattr(module, class_name)
except AttributeError:
msg = 'Module "%s" does not define a "%s" attribute/class' % (
module_path, class_name)
six.reraise(ImportError, ImportError(msg), sys.exc_info()[2])
你可以像import_string("module_path.to.all.the.way.to.your_class")一样使用它。