python父类的类成员怎么定义_python如何找到哪些父类定义子对象的方法
属性通常不属于任何类。属性通常属于它们所属的对象。在
然而,方法与定义它们的类密切相关。在
考虑一下这个程序: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和{}中的每一个都与绑定到o的对象相关联。在任何技术意义上,它们都不涉及任何特定的类。在
然而,方法create_attrib_a和{}精确地携带了您想要的信息。了解^{}模块如何检索其定义的文件名和行号。在
总结
以上是生活随笔为你收集整理的python父类的类成员怎么定义_python如何找到哪些父类定义子对象的方法的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: telnet 22正常 ssh无法连接_
- 下一篇: python list查找元素下标_Py