Skip to content
UPEEC

Polymorphism

Different classes respond to the same method call in their own way.

Easy
Comments 0
class Led:    
    def power(self): return 0.06
class Motor:  
    def power(self): return 12.0

for device in (Led(), Motor()):
    print(device.power())      # works for any object with .power()

In Python this is mostly duck typing: "if it walks like a duck…".

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.