python父类的类成员怎么定义_python如何找到哪些父类定义子对象的方法
屬性通常不屬于任何類。屬性通常屬于它們所屬的對(duì)象。在
然而,方法與定義它們的類密切相關(guān)。在
考慮一下這個(gè)程序:class base(object):
def create_attrib_a(self):
self.a = 1
class derived(base):
def create_attrib_b(self):
self.b = 1
def create_attrib_c(obj):
obj.c = 1
import inspect
o = derived()
o.create_attrib_a()
o.create_attrib_b()
create_attrib_c(o)
o.d = 1
# The objects attributes are relatively anonymous
print o.__dict__
# But the class's methods have lots of information available
for name, value in inspect.getmembers(o, inspect.ismethod):
print 'Method=%s, filename=%s, line number=%d'%(
name,
value.im_func.func_code.co_filename,
value.im_func.func_code.co_firstlineno)
如您所見,屬性a、b、c和{}中的每一個(gè)都與綁定到o的對(duì)象相關(guān)聯(lián)。在任何技術(shù)意義上,它們都不涉及任何特定的類。在
然而,方法create_attrib_a和{}精確地?cái)y帶了您想要的信息。了解^{}模塊如何檢索其定義的文件名和行號(hào)。在
總結(jié)
以上是生活随笔為你收集整理的python父类的类成员怎么定义_python如何找到哪些父类定义子对象的方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: telnet 22正常 ssh无法连接_
- 下一篇: python list查找元素下标_Py