繁体   English   中英

我如何从方法列表中得知哪些方法已在该类中定义,哪些已被继承?

[英]How do i tell from a list of methods, which ones have been defined in that class and those that have been inherited?

我如何从方法列表中得知哪些方法已在该类中定义,哪些已被继承? 例如。 我有一个具有两个类的银行模块,BankAccount和Transactions。 交易继承自BankAccount

>>> dir(bank.BankAccount) 
[ 'chk', 'interest']
>>> dir(bank.Transcations) 
[ 'chk', 'deposit', 'interest', 'withdraw']

我如何检查是否继承了“ chk”和“兴趣”方法,是否可以知道从哪个类继承了?

>>> class BankAccount(object):
...     def deposit(self):
...             pass
...     def account_no(self):
...             pass
... 
>>> class Transaction(BankAccount):
...     def withdrawal(self):
...             pass
... 
>>> Transaction.__dict__.keys()
['__module__', '__doc__', 'withdrawal']
>>> BankAccount.__dict__.keys()
['__module__', 'deposit', '__dict__', '__weakref__', '__doc__', 'account_no']

您可以检查bank.Transactions.__dict__.keys()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM