Python Tutorial
class DerivedClass(BaseClass): statement1 . . . statementNBaseclass should be defined in the same scope containing the derived class definition.
class DerivedClass(module_name.BaseClassName):
super().method_name(arguments);
class Parent: def __init__(self, a, b): self.a = a; self.b = b; def showab(self): print("a and b: %d %d" % (self.a, self.b)); # Inheritance Child is the subclass and Parent is the base class class Child(Parent): def __init__(self, a, b, c): super().__init__(a, b); self.c = c; def showc(self): print("c: %d" % self.c); def sum(self): print("sum(a+b+c): %d" % (self.a+self.b+self.c)); superObj = Parent(20, 30); subObj = Child(11, 12, 13); superObj.showab(); subObj.showab(); subObj.showc(); subObj.sum();Output:
$ python3 inheritance_test.py a and b: 20 30 a and b: 11 12 c: 13 sum(a+b+c): 36
class DerivedClass(Base1, Base2, Base3): statement1 . . . statementN
class Parent1: def __init__(self, a, b): self.a = a; self.b = b; def showab(self): print("a and b: %d %d" % (self.a, self.b)); class Parent2: def __init__(self, c, d): self.c = c; self.d = d; def showcd(self): print("c and d: %d %d" % (self.c, self.d)); # multiple inheritance class Child(Parent1, Parent2): def __init__(self, a, b, c, d, e): Parent1.__init__(self, a, b); Parent2.__init__(self, c, d); self.e = e; def showc(self): print("e: %d" % self.e); def sum(self): print("sum(a+b+c+d+e): %d" % (self.a+self.b+self.c+self.d+self.e)); superObj1 = Parent1(20, 30); superObj2 = Parent2(40, 50); subObj = Child(11, 12, 13, 14, 15); superObj1.showab(); superObj2.showcd(); subObj.showab(); subObj.showcd(); subObj.showc(); subObj.sum();Output:
$ python3 inheritance_test1.py a and b: 20 30 c and d: 40 50 a and b: 11 12 c and d: 13 14 e: 15 sum(a+b+c+d+e): 65
isinstance(obj, class1)True only if obj.__class__ is class1 or some class derived from class1.
issubclass(class1, class2)True since class1 is a subclass of class2.
issubclass(class1, class2)False since class1 is not a subclass of class2.
class Parent1: def __init__(self, a, b): self.a = a; self.b = b; def showab(self): print("a and b: %d %d" % (self.a, self.b)); class Parent2: def __init__(self, c, d): self.c = c; self.d = d; def showcd(self): print("c and d: %d %d" % (self.c, self.d)); class Child(Parent1, Parent2): def __init__(self, a, b, c, d, e): Parent1.__init__(self, a, b); Parent2.__init__(self, c, d); self.e = e; def showc(self): print("e: %d" % self.e); def sum(self): print("sum(a+b+c+d+e): %d" % (self.a+self.b+self.c+self.d+self.e)); superObj1 = Parent1(20, 30); superObj2 = Parent2(40, 50); subObj = Child(11, 12, 13, 14, 15); if isinstance(superObj1, Parent1): print("superObj1 is instance of Class Parent1"); if issubclass(Child, Parent1): print("Child class is sub class of Class Parent1"); if issubclass(Child, Parent2): print("Child class is sub class of Class Parent2");Output:
$ python3 inheritance_test1.py superObj1 is instance of Class Parent1 Child class is sub class of Class Parent1 Child class is sub class of Class Parent2
Python Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page