In [1]:
class Base1:
pass
class Base2:
pass
class MultiDerived(Base1, Base2):
pass
In [3]:
class Base1:
def test(self):
print("in Base1 -> test")
class Base2:
def test(self):
print("in Base2 -> test")
class MultiDerived(Base1, Base2):
def test2(self):
super().test()
Base2.test(Base2)
class MultiDerived2(Base2, Base1):
def test2(self):
super().test()
Base2.test(Base2)
print("Please check the result of test()")
# d = Base2()
# print(type(d))
md = MultiDerived()
md.test2()
md.test()
print("*"*10)
md2 = MultiDerived2()
md2.test2()
md2.test()
In [ ]:
In [ ]:
In [ ]:
class Base:
pass
class Derived1(Base):
pass
class Derived2(Derived1):
pass
In the multiple inheritance scenario, any specified attribute is searched first in the current class. If not found, the search continues into parent classes in depth-first, left-right fashion without searching same class twice
In [1]:
class Base:
def test(self):
print("In Base test")
def test_alone(self):
print("In Base test: test_alone")
class Derived1(Base):
def test(self):
print("In Derived1 test")
super().test()
def test_alone(self, val):
print("In Derived1 test: test_alone ", val)
# def test_alone(self):
# print("In Base test: test_alone")
class Derived2(Derived1):
def test2(self):
print("in Derived2 test2")
obj = Derived2()
obj.test()
obj.test2()
obj.test_alone()
Base.test(Base)
In [9]:
class Base:
def test(self):
print("In Base test")
def test_base(self):
print("test_base")
def test_alone(self):
print("In Base test: test_alone")
class Derived1(Base):
def test(self):
print("In Derived1 test")
super().test()
self.test_base()
def test_alone(self, val):
print("In Derived1 test: test_alone ", val)
self.test()
obj = Derived1()
obj.test()
obj.test_alone("test")
Base.test(Base)
In [11]:
class Base:
def test(self):
print("In Base test")
def test_base(self):
print("test_base")
def test_alone(self):
print("In Base test: test_alone")
class Derived1(Base):
def test(self):
print("In Derived1 test")
super().test()
Base.test_base(Base)
def test_alone(self, val):
print("In Derived1 test: test_alone ", val)
self.test()
obj = Derived1()
obj.test()
obj.test_alone("test")
Base.test(Base)
In [6]:
class Base:
def test(self):
print("In Base test")
def test_alone(self):
print("In Base test: test_alone")
class Derived1(Base):
def test(self):
print("In Derived1 test")
super().test()
def test_alone(self, val):
print("In Derived1 test: test_alone ", val)
def test_alone(self):
print("In Base test: test_alone")
class Derived2(Derived1):
def test2(self):
print("in Derived2 test2")
obj = Derived2()
obj.test()
obj.test2()
obj.test_alone()
Base.test(Base)
In [3]:
class Base():
def test1(self):
print("In Base test")
class Derived1(Base):
def test(self):
print("In Derived1 test")
class Derived3(Derived1):
pass
d = Derived3()
d.test()
d.test1()
In [27]:
class Base():
def test(self):
print("In Base test")
class Derived1(Base):
def test(self):
print("In Derived1 test", end=", ")
return "Golu"
class Derived3(Derived1):
pass
d = Derived3()
print(d.test())
In [39]:
#### Explicitly calling function
class Base:
def test(self):
print("In Base test")
class Derived1(Base):
def test(self):
print("In Derived1 test")
class Derived2(Derived1):
pass
obj = Derived2
obj.test(obj)
Derived2.test(Derived2)
In [41]:
#### Explicitly calling function
class Base:
def test(self):
print("In Base test")
class Derived1(Base):
def test(self):
print("In Derived1 test")
print(type(self))
class Derived2(Derived1):
pass
obj = Derived2
obj.test(obj)
Derived2.test(Derived2)
In [61]:
#### Explicitly calling function
class Base:
def test(self):
print("In Base test")
class Derived1(Base):
def test(self):
print("In Derived1 test")
print(type(self))
class Derived2(Derived1):
pass
obj = Derived2()
obj.test()
Derived2.test(Derived2)
In [ ]: