DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Class Inheritance in Python


In Python you have to call initializing method of super class explicitly:

$ python -V
Python 3.6.3

$ cat << EOF > demo.py
class A:
    def __init__(self, pa, pb):
        print('init in class A')
        self.pa = pa
        self.pb = pb

class B(A):
    def __init__(self, pa, pb):
        print("init in class B")
        super().__init__(pa, pb)

b = B(3, 2)
print(b.pa + b.pb)
EOF

$ python demo.py
init in class B
init in class A
5

Ref: https://stackoverflow.com/questions/2399307/how-to-invoke-the-super-constructor



Published

Dec 29, 2017

Last Updated

Dec 29, 2017

Category

Tech

Tags

  • class 4
  • inheritance 1
  • python 136

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor