python获取对象的类名_如何找到对象的类名? (是Python文档)

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 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__

(, )

type(obj)

type(obj).mro()

[, , , , ] 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()

[, , ,

, ]

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

this idiom to the cookbook.

--- Heiko.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值