class A(object): def show(self): print('base show') class B(A): def show(self): print('derived show') obj = B() obj.show()
问题:请问如何修改以上代码,使得以上的代码调用类A的show方法?
class A(object): title = 'This is A object' def show(self): print('base show') class B(A): def show(self): print('derived show') obj = B() obj.__class__ = A # 指定obj的class类为A obj.show() # base show print(obj.__class__) # <class '__main__.A'> print(obj.title) # This is A object # 拓展 # 把obj的__class__改为A之后可以就可以调用类A的所有属性和方法