我必须像下面这样上课。
class Reports()
_name = 'reports'
def fetch_transaction_invoice():
#any logic here
class Bar():
_name = 'bar'
def test_method():
# here i want to access fetch_transaction_invoice of Reports class.
但是当我尝试跟随
class Bar():
_name = 'bar'
def test_method():
Reports.fetch_transaction_invoice ()
它给了我下面的错误。
TypeError: unbound method fetch_transaction_invoice() must be called with Reports instance as first argument (got nothing instead)
答案 0 :(得分:0)
以下通过使用@classmethod
装饰器起作用:
class Reports():
_name = 'reports'
@classmethod
def fetch_transaction_invoice(cls):
print('fetched')
class Bar():
_name = 'bar'
def test_method(self):
Reports.fetch_transaction_invoice ()
Bar().test_method()
打印
fetched
(由于缩进问题,我不得不对您要尝试的内容进行一些假设。)