shaz
shaz

Reputation: 71

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

How do i tell from a list of methods, which ones have been defined in that class and those that have been inherited? Eg. i have a bank module that has two classes, BankAccount and Transactions. Transcations inherits from BankAccount

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

how do i check that 'chk' and 'interest' methods are inherited and is it possible to tell from which class there were inherited from?

Upvotes: 2

Views: 81

Answers (2)

user225312
user225312

Reputation: 131637

>>> 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']

Upvotes: 3

6502
6502

Reputation: 114481

You can check bank.Transactions.__dict__.keys()

Upvotes: 1

Related Questions