More Related Content
More from P3 InfoTech Solutions Pvt. Ltd. (18)
Python Programming Essentials - M33 - Duck Typing
- 2. © SkillBrew http://skillbrew.com
What is Duck Typing
“When I see a bird that walks like a duck
and swims like a duck and quacks like a
duck, I call that bird a duck.”
A style of typing in which an
object's methods and properties determine
the valid semantics, rather than
its inheritance from a particular class or
implementation of an explicit interface.
- 3. © SkillBrew http://skillbrew.com
Duck Typing Example
class Duck:
def quack(self):
print "Quack, quack!“
def fly(self):
print "Flap, Flap!“
class Person:
def quack(self):
print "I'm Quackin'!“
def fly(self):
print "I'm Flyin'!“
def in_the_forest(thing):
thing.quack()
thing.fly()
in_the_forest(Duck())
in_the_forest(Person())