forked from abhi1540/PythonConceptExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduckTyping.py
More file actions
35 lines (24 loc) · 730 Bytes
/
duckTyping.py
File metadata and controls
35 lines (24 loc) · 730 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Duck Typing and Easier to ask forgiveness than permission (EAFP)
class Duck:
def quack(self):
print('Quack, quack')
def fly(self):
print('Flap, Flap!')
class Person:
def quack(self):
print("I'm Quacking Like a Duck!")
def fly(self):
print("I'm Flapping my Arms!")
# it doesn't matter what object(here thing) u are passing unless it has same methods
def quack_and_fly(thing):
try:
thing.quack()
thing.fly()
thing.bark()
except AttributeError as e:
print(e)
d = Duck()
p = Person()
# we don't worry which object it is, if that object has same methods then it should execute
quack_and_fly(d)
quack_and_fly(p)