动态类/模块使用? (比如Java的forName) [英] dynamic class/module use? (like Java's forName)

查看:45
本文介绍了动态类/模块使用? (比如Java的forName)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python是否提供了动态使用模块和/或类的方法?

我正在思考Java的Class.forName。


作为伪代码示例,我正在寻找以下能力:

classIWantToInstantiate =" packagenamehere.classNameHere"


anInstanceOfThatClass = makeNewClassFromString (classIWantToInstantiate,

参数[])


#now anInstanceOfThatClass指的是一个即时的
#packagenamehere.classNameHere对象!


谢谢

alex

解决方案

Alex Hunsley写道:
< blockquote class =post_quotes> python是否提供了动态使用模块和/或类的方法?
我正在思考Java的Class.forName。

作为伪代码示例,我正在寻找以下能力:

classIWantToInstantiate =" packagenamehere.classNameHere"

anInstanceOfThatClass = makeNewClassFromString(classIWantToInstantiate,
parameters [])
#now anInstanceOfThatClass指的是一个即时的
#packagenamehere.classNameHere对象!

谢谢
alex




哎呀..我瞪眼但显然不够努力!我找到了答案

google groups:

http://shorterlink.com/?N2HPGU


alex


文章< _O ** ***************@fe2.news.blueyonder.co.uk> ;,

Alex Hunsley< la ** @ tardis.ed.ac.molar .UK>写道:

python是否提供了动态使用模块和/或类的方法?
我正在思考Java的Class.forName。




出于好奇,你为什么要这样做?


你提供的谷歌链接谈到了eval和exec 。我的经验

是当我第一次开始使用Python(大约7年前)时,我使用了

eval和exec。现在,我不记得上次我曾经想要

使用它们。


你能给我们一个例子说明你的意思吗?试着做什么?我的猜测,

还没有看到它,可能是一个更好的方法来做它比你要问的b $ b。


2004年7月17日星期六18:00:26格林尼治标准时间,谣言说Alex Hunsley

< la ** @ tardis.ed.ac.molar .UK>可能写的:

python是否提供了一种动态使用模块和/或类的方法?
我正在思考Java的Class.forName 。




[snip]


您是否考虑过新模块?如果没有,试一试。

-

TZOTZIOY,我说英格兰最好,

Tssss! - 布拉德皮特作为前所未有的古希腊语的阿基里斯


Does python provide a way to dynamically use modules and/or classes?
I''m thinking in the vein of Java''s Class.forName.

As a pseudocode example, I''m looking for the following ability:
classIWantToInstantiate = "packagenamehere.classNameHere"

anInstanceOfThatClass = makeNewClassFromString(classIWantToInstantiate,
parameters[])

# now anInstanceOfThatClass refers to an instantied
# packagenamehere.classNameHere object!

thanks
alex

解决方案

Alex Hunsley wrote:

Does python provide a way to dynamically use modules and/or classes?
I''m thinking in the vein of Java''s Class.forName.

As a pseudocode example, I''m looking for the following ability:
classIWantToInstantiate = "packagenamehere.classNameHere"

anInstanceOfThatClass = makeNewClassFromString(classIWantToInstantiate,
parameters[])

# now anInstanceOfThatClass refers to an instantied
# packagenamehere.classNameHere object!

thanks
alex



Oops.. I goggled but not hard enough obviously! I found my answer on
google groups:

http://shorterlink.com/?N2HPGU

alex


In article <_O*****************@fe2.news.blueyonder.co.uk>,
Alex Hunsley <la**@tardis.ed.ac.molar.uk> wrote:

Does python provide a way to dynamically use modules and/or classes?
I''m thinking in the vein of Java''s Class.forName.



Just out of curiosity, why do you want to do that?

The google link you provided talked about eval and exec. My experience
is that when I first started using Python (about 7 years ago), I used
eval and exec a lot. Now, I can''t remember the last time I ever wanted
to use either.

Can you give us an example of what you''re trying to do? My guess,
having not yet seen it, is that there''s probably a better way to do it
than what you''re asking.


On Sat, 17 Jul 2004 18:00:26 GMT, rumours say that Alex Hunsley
<la**@tardis.ed.ac.molar.uk> might have written:

Does python provide a way to dynamically use modules and/or classes?
I''m thinking in the vein of Java''s Class.forName.



[snip]

Have you already considered the ''new'' module? If not, give it a try.
--
TZOTZIOY, I speak England very best,
"Tssss!" --Brad Pitt as Achilles in unprecedented Ancient Greek


这篇关于动态类/模块使用? (比如Java的forName)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆