Python论坛  - 讨论区

标题:[python-chinese] Python中如何实现类似Java中反射的功能?

2006年02月23日 星期四 12:56

ajax chelsea ajaxchelsea at gmail.com
Thu Feb 23 12:56:16 HKT 2006

就是根据一个字符串形式的类名创建这个类的实例? 根据一个字符串形式的方法名调用这个方法?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060223/c5b2917e/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月23日 星期四 13:16

超群李 achilles.lee at gmail.com
Thu Feb 23 13:16:48 HKT 2006

getattr(modulename,classname)

On 2/23/06, ajax chelsea <ajaxchelsea at gmail.com> wrote:
>
> 就是根据一个字符串形式的类名创建这个类的实例? 根据一个字符串形式的方法名调用这个方法?
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


--
李超群
mobile phone:13759961869
office phone:029-87607341
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060223/d682b6c0/attachment-0001.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月23日 星期四 13:17

超群李 achilles.lee at gmail.com
Thu Feb 23 13:17:20 HKT 2006

invoke方法也一样。都是用getattr(class,attributename)

On 2/23/06, 超群李 <achilles.lee at gmail.com> wrote:
>
> getattr(modulename,classname)
>
> On 2/23/06, ajax chelsea <ajaxchelsea at gmail.com> wrote:
> >
> > 就是根据一个字符串形式的类名创建这个类的实例? 根据一个字符串形式的方法名调用这个方法?
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
>
> --
> 李超群
> mobile phone:13759961869
> office phone:029-87607341
>



--
李超群
mobile phone:13759961869
office phone:029-87607341
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060223/5ef5b3e5/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月23日 星期四 13:25

ajax chelsea ajaxchelsea at gmail.com
Thu Feb 23 13:25:45 HKT 2006

ok,是这样吗:

my_class_def = getattr("mymodule", "MyClass")

inst = my_class_def( some params for __init__ of MyClass)

然后inst就是MyClass的实例了?


在06-2-23,超群李 <achilles.lee at gmail.com> 写道:
>
> invoke方法也一样。都是用getattr(class,attributename)
>
> On 2/23/06, 超群李 <achilles.lee at gmail.com> wrote:
> >
> > getattr(modulename,classname)
> >
> > On 2/23/06, ajax chelsea < ajaxchelsea at gmail.com> wrote:
> > >
> > > 就是根据一个字符串形式的类名创建这个类的实例? 根据一个字符串形式的方法名调用这个方法?
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese at lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request at lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> >
> >
> > --
> > 李超群
> > mobile phone:13759961869
> > office phone:029-87607341
> >
>
>
>
> --
> 李超群
> mobile phone:13759961869
> office phone:029-87607341
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060223/69f463ba/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月23日 星期四 13:30

超群李 achilles.lee at gmail.com
Thu Feb 23 13:30:55 HKT 2006

对。


On 2/23/06, ajax chelsea <ajaxchelsea at gmail.com> wrote:
>
> ok,是这样吗:
>
> my_class_def = getattr("mymodule", "MyClass")
>
> inst = my_class_def( some params for __init__ of MyClass)
>
> 然后inst就是MyClass的实例了?
>
>
> 在06-2-23,超群李 <achilles.lee at gmail.com> 写道:
> >
> > invoke方法也一样。都是用getattr(class,attributename)
> >
> > On 2/23/06, 超群李 <achilles.lee at gmail.com > wrote:
> > >
> > > getattr(modulename,classname)
> > >
> > > On 2/23/06, ajax chelsea < ajaxchelsea at gmail.com > wrote:
> > > >
> > > > 就是根据一个字符串形式的类名创建这个类的实例? 根据一个字符串形式的方法名调用这个方法?
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese at lists.python.cn
> > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > > Unsubscribe: send unsubscribe to   python-chinese-request at lists.python.cn
> > > >
> > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > > >
> > > >
> > >
> > >
> > > --
> > > 李超群
> > > mobile phone:13759961869
> > > office phone:029-87607341
> > >
> >
> >
> >
> > --
> > 李超群
> > mobile phone:13759961869
> > office phone:029-87607341
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


--
李超群
mobile phone:13759961869
office phone:029-87607341
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060223/2cb797c9/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月23日 星期四 19:02

唐晓平 xiaoping.tang at gmail.com
Thu Feb 23 19:02:27 HKT 2006

主意下 my_class_def = getattr(mymodule, "MyClass")

mymodule 不可以用字符串表达


On 2/23/06, ajax chelsea <ajaxchelsea at gmail.com> wrote:
>
> ok,是这样吗:
>
> my_class_def = getattr("mymodule", "MyClass")
>
> inst = my_class_def( some params for __init__ of MyClass)
>
> 然后inst就是MyClass的实例了?
>
>
> 在06-2-23,超群李 <achilles.lee at gmail.com> 写道:
> >
> > invoke方法也一样。都是用getattr(class,attributename)
> >
> > On 2/23/06, 超群李 <achilles.lee at gmail.com > wrote:
> > >
> > > getattr(modulename,classname)
> > >
> > > On 2/23/06, ajax chelsea < ajaxchelsea at gmail.com > wrote:
> > > >
> > > > 就是根据一个字符串形式的类名创建这个类的实例? 根据一个字符串形式的方法名调用这个方法?
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese at lists.python.cn
> > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > > Unsubscribe: send unsubscribe to   python-chinese-request at lists.python.cn
> > > >
> > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > > >
> > > >
> > >
> > >
> > > --
> > > 李超群
> > > mobile phone:13759961869
> > > office phone:029-87607341
> > >
> >
> >
> >
> > --
> > 李超群
> > mobile phone:13759961869
> > office phone:029-87607341
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060223/0ca0c8eb/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月23日 星期四 21:13

limodou limodou at gmail.com
Thu Feb 23 21:13:16 HKT 2006

On 2/23/06, ajax chelsea <ajaxchelsea at gmail.com> wrote:
> 就是根据一个字符串形式的类名创建这个类的实例? 根据一个字符串形式的方法名调用这个方法?

使用__import__可以根据一个字符串导入一个模块,而这个模块就是一个普通的模块。然后再通过名字从模块中取出真正的类。使用getattr()即可。

--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号