如何找到对象的类名? (是Python文档) [英] How to find the classname of an object? (was Python Documentation)

查看:286
本文介绍了如何找到对象的类名? (是Python文档)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上也想要所有的父类。所以,如果D派生出来,那么从B派生出来,我最终需要一个元组(D,C,B,A )。


对于那些关注Python文档线程的人来说,这是一本很好的例子,说明PHP手册是如何更好的。我发现如何在PHP中用几个b $ b秒做这个。我在Python文档中搜索了class name,classname,

" introspection"和getclass。我查看了

教程的Class部分以及Programming FAQ。 PHP手册的相关函数

部分非常有用。如果在内置函数isinstance()或issubclass()的

部分中,对于相关函数有一个

部分会很酷。这会指向getclassname(obj)

(如果存在)。


感谢您的帮助。


- C

解决方案

这将获得一个对象类的名称


obj .__ class __.__ name__


这将返回其基类的元组


obj .__ class __.__ bases__


Christopher J. Bottaro写道:

我实际上也想要所有的父类。因此,如果D派生出来,那么从B派生出来,我最终需要一个元组(D,C,B,A)。对于那些关注Python文档线程的人来说,这是一个很好的例子,说明PHP手册是如何更好的。我在PHP中用几秒钟的时间找到了如何做到这一点。我在Python文档中搜索了class name,classname,
introspection。和getclass。我还查看了
教程的Class部分以及Programming FAQ。 PHP手册的相关功能部分非常有用。如果在内置函数isinstance()或issubclass()的
部分中存在相关函数的部分,那将会很酷。这会指向getclassname(obj)
(如果存在)。

感谢您的帮助。

- C


2005年5月12日星期四23:30:21 GMT,Farshid Lashkari< la ******** @ SPAMworldviz.com>写道:

这将得到一个对象类的名称

obj .__ class __.__ name__

这将返回一个元组它的基类

obj .__ class __.__ bases__


但不是它继承的所有基类,例如

C类(对象):传递
...类B1(C):传递
...类B2(C ):传递
... A级(B1,B2):传递
... obj = A()
obj .__ class __.__ name__
''A''obj .__ class __.__ bases__
(< class''__ main __。B1''>,< class''__ main __。B2''>)

type(obj)
< class''__ main __。A''> type(obj).mro()
[< class''__ main __。A''>,< class''__ main __。B1''>,< class''__ main __。B2''> ;,< class''__ main __。C''>,< type''object''>] tuple(x .__ name__ for x in type(obj).mro())



(''''',''B1'',''B2'',''C'',''对象'')

我实际上也想要所有的父类。因此,如果D派生出来,那么从B派生出来,我最终需要一个元组(D,C,B,A)。对于那些关注Python文档线程的人来说,这是一个很好的例子,说明PHP手册是如何更好的。我在PHP中用几秒钟的时间找到了如何做到这一点。我在Python文档中搜索了class name,classname,
introspection。和getclass。我还查看了
教程的Class部分以及Programming FAQ。 PHP手册的相关功能部分非常有用。如果在内置函数isinstance()或issubclass()的
部分中存在相关函数的部分,那将会很酷。这会指向getclassname(obj)
(如果存在)。

感谢您的帮助。

- C




问候,

Bengt Richter


2005年5月13日星期五03:11,Bengt Richter写道:

>>> type(obj).mro()



[< class''__ main __。A''>,< class''__ main __。B1''> ,< class''__ main __。B2''>,
< class''__ main __。C''>,< type''object''>]




哇!不需要编写深度优先的树遍历算法......有人在菜谱上添加了这个成语。


--- Heiko。


I actually want all the parent classes too. So if D derives off C derives
off B derives off A, I ultimately want a tuple (''D'', ''C'', ''B'', ''A'').

For those of you following the Python Documentation thread, this is a good
example of how the PHP manual is "better". I found how to do this in a few
seconds in PHP. I searched the Python docs for "class name", "classname",
"introspection" and "getclass". I looked in the Class section of the
tutorial also and also the Programming FAQ. The "related functions"
section of the PHP manual is really helpful. It would be cool if in the
section for the built-in function isinstance() or issubclass() there is a
section for "related functions" that would point me to getclassname(obj)
(if it exists).

Thanks for the help.

-- C

解决方案

This will get the name of an objects class

obj.__class__.__name__

This will return a tuple of its base classes

obj.__class__.__bases__

Christopher J. Bottaro wrote:

I actually want all the parent classes too. So if D derives off C derives
off B derives off A, I ultimately want a tuple (''D'', ''C'', ''B'', ''A'').

For those of you following the Python Documentation thread, this is a good
example of how the PHP manual is "better". I found how to do this in a few
seconds in PHP. I searched the Python docs for "class name", "classname",
"introspection" and "getclass". I looked in the Class section of the
tutorial also and also the Programming FAQ. The "related functions"
section of the PHP manual is really helpful. It would be cool if in the
section for the built-in function isinstance() or issubclass() there is a
section for "related functions" that would point me to getclassname(obj)
(if it exists).

Thanks for the help.

-- C



On Thu, 12 May 2005 23:30:21 GMT, Farshid Lashkari <la********@SPAMworldviz.com> wrote:

This will get the name of an objects class

obj.__class__.__name__

This will return a tuple of its base classes

obj.__class__.__bases__
But not all base classes that it inherits from, e.g.,

class C(object): pass ... class B1(C): pass ... class B2(C): pass ... class A(B1, B2): pass ... obj = A()
obj.__class__.__name__ ''A'' obj.__class__.__bases__ (<class ''__main__.B1''>, <class ''__main__.B2''>)
type(obj) <class ''__main__.A''> type(obj).mro() [<class ''__main__.A''>, <class ''__main__.B1''>, <class ''__main__.B2''>, <class ''__main__.C''>, <type ''object''>] tuple(x.__name__ for x in type(obj).mro())


(''A'', ''B1'', ''B2'', ''C'', ''object'')

Christopher J. Bottaro wrote:

I actually want all the parent classes too. So if D derives off C derives
off B derives off A, I ultimately want a tuple (''D'', ''C'', ''B'', ''A'').

For those of you following the Python Documentation thread, this is a good
example of how the PHP manual is "better". I found how to do this in a few
seconds in PHP. I searched the Python docs for "class name", "classname",
"introspection" and "getclass". I looked in the Class section of the
tutorial also and also the Programming FAQ. The "related functions"
section of the PHP manual is really helpful. It would be cool if in the
section for the built-in function isinstance() or issubclass() there is a
section for "related functions" that would point me to getclassname(obj)
(if it exists).

Thanks for the help.

-- C



Regards,
Bengt Richter


On Friday 13 May 2005 03:11, Bengt Richter wrote:

>>> type(obj).mro()



[<class ''__main__.A''>, <class ''__main__.B1''>, <class ''__main__.B2''>,
<class ''__main__.C''>, <type ''object''>]



Wow! No need to write a depth-first tree-traversal algorithm... Somebody add
this idiom to the cookbook.

--- Heiko.


这篇关于如何找到对象的类名? (是Python文档)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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