I Spy Code - Python

Subclass Inherits Method

Here is a python example that shows how a sub class inherits a method:

Source: (example.py)

class Pet:
  sound = "none"
  def speak(self):
    print(self.sound)
 
class Dog(Pet):
  sound = "bark"
 
 
p = Pet()
p.speak()
 
d = Dog()
d.speak()
 

Output:

$ python example.py
none
bark


Related Examples

Call Methods Dynamically
Docstring
Initializing Object Variables
Method Arguments
Subclass Inherits Method
Test For Null Object
hqdwppls.php

7