DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Call Method in Parent Class in Python


You can call the method of the parent class by its name directly:

$ cat callpm.py
class A():
    def bar(self, string):
        print("Hi, I'm bar, inherited from A" + string)

class B(A):
    def baz(self):
        self.bar(" - called by baz in B")

B().baz()

$  python --version
Python 3.6.3 :: Anaconda, Inc.

$ python callpm.py
Hi, I'm bar, inherited from A - called by baz in B


Published

Feb 5, 2018

Last Updated

Feb 5, 2018

Category

Tech

Tags

  • hierarchy 1
  • python 136
  • super class 1

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor