对于初学者,do_that()
不返回任何东西。所以调用它几乎没有任何作用。
self.B = B.do_that()
也不会工作。您必须首先创建该类的一个实例B
:
mything = B(your_parameters)
mything.do_that()
如果你希望它返回一些东西(即元组),你应该将你的更改do_that()
为:
def do_that(self):
return (1, 2)
最后一点,这都可以通过继承来实现:
class A(B): # Inherits Class B
def __init__(self,master):
"""Some work here"""
def do_this(self):
print self.do_that()[1] # This is assuming the do_that() function returns that tuple
使用继承方法:
>>> class B:
... def __init__(self, master):
... """Some work here"""
... def do_that(self):
... return (1,2)
...
>>> class A(B):
... def __init__(self, master):
... """Some work here"""
... def do_this(self):
... print self.do_that()[1]
...
>>> mything = A('placeholder')
>>> mything.do_this()
2