python有一个等价于Java Class.forName()? [英] Does python have an equivalent to Java Class.forName()?
问题描述
我需要接受一个字符串参数,并在Python中创建一个在该字符串中命名的类的对象。在Java中,我将使用 Class.forName()。newInstance()
。在Python中是否有等效项?
I have the need to take a string argument and create an object of the class named in that string in Python. In Java, I would use Class.forName().newInstance()
. Is there an equivalent in Python?
感谢您的回应。回答那些想知道我在做什么:我想使用命令行参数作为类名,并实例化它。我实际上是在Jython编程和实例化Java类,因此Java问题的Java。 getattr()
工作得很好。非常感谢。
Thanks for the responses. To answer those who want to know what I'm doing: I want to use a command line argument as the class name, and instantiate it. I'm actually programming in Jython and instantiating Java classes, hence the Java-ness of the question. getattr()
works great. Thanks much.
推荐答案
Python中的反射比Java更容易和灵活得多。
Reflection in python is a lot easier and far more flexible than it is in Java.
我建议您阅读此教程
没有直接函数(我知道),它接受一个完全限定的类名并返回该类,但是你有所有需要构建的函数,你可以连接它们
There's no direct function (that I know of) which takes a fully qualified class name and returns the class, however you have all the pieces needed to build that, and you can connect them together.
有一点建议:当你在python时,不要尝试以Java风格编程。
One bit of advice though: don't try to program in Java style when you're in python.
如果你能解释你想要做什么,也许我们可以帮你找到更多的pythonic方式。
If you can explain what is it that you're trying to do, maybe we can help you find a more pythonic way of doing it.
这里是一个函数,它做你想要的:
Here's a function that does what you want:
def get_class( kls ):
parts = kls.split('.')
module = ".".join(parts[:-1])
m = __import__( module )
for comp in parts[1:]:
m = getattr(m, comp)
return m
您可以使用此函数的返回值,
You can use the return value of this function as if it were the class itself.
这里有一个用法示例:
>>> D = get_class("datetime.datetime")
>>> D
<type 'datetime.datetime'>
>>> D.now()
datetime.datetime(2009, 1, 17, 2, 15, 58, 883000)
>>> a = D( 2010, 4, 22 )
>>> a
datetime.datetime(2010, 4, 22, 0, 0)
>>>
如何运作?
我们使用 __ import __
来导入保存类的模块,这需要我们首先从完全限定名称中提取模块名称。然后我们导入模块:
We're using __import__
to import the module that holds the class, which required that we first extract the module name from the fully qualified name. Then we import the module:
m = __import__( module )
在这种情况下, m
只会引用顶层模块,
In this case, m
will only refer to the top level module,
例如,如果你的类存在于 foo.baz
模块中,则 m
模块 foo
我们可以很容易地使用<$ c获得 foo.baz
的引用$ c> getattr(m,'baz')
For example, if your class lives in foo.baz
module, then m
will be the module foo
We can easily obtain a reference to foo.baz
using getattr( m, 'baz' )
要从顶级模块到类,必须递归使用 gettatr
对类名的部分
To get from the top level module to the class, have to recursively use gettatr
on the parts of the class name
例如,如果类名为 foo .baz.bar.Model
然后我们这样做:
Say for example, if you class name is foo.baz.bar.Model
then we do this:
m = __import__( "foo.baz.bar" ) #m is package foo
m = getattr( m, "baz" ) #m is package baz
m = getattr( m, "bar" ) #m is module bar
m = getattr( m, "Model" ) #m is class Model
for comp in parts[1:]:
m = getattr(m, comp)
在循环结束时, m
类。这意味着 m
实际上是类的itslef,你可以做的例子:
At the end of the loop, m
will be a reference to the class. This means that m
is actually the class itslef, you can do for instance:
a = m() #instantiate a new instance of the class
b = m( arg1, arg2 ) # pass arguments to the constructor
这篇关于python有一个等价于Java Class.forName()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!