Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

duck typing in python

# Python program to demonstrate
# duck typing
  
  
class Bird:
    def fly(self):
        print("fly with wings")
  
class Airplane:
    def fly(self):
        print("fly with fuel")
  
class Fish:
    def swim(self):
        print("fish swim in sea")
  
# Attributes having same name are
# considered as duck typing
for obj in Bird(), Airplane(), Fish():
    obj.fly()
#output
#fly with wings
#fly with fuel
#Traceback (most recent call last):
  #File "/home/854855e5570b9ce4a9e984209b6a1c21.py", line 20, in 
    #obj.fly()
#AttributeError: 'Fish' object has no attribute 'fly'
In this example, we can see a class supports some method we can modify it
or give them new functionality. Duck-typing emphasis what the 
object can really do, rather than what the object is.
Comment

duck typing in python

class Duck:
    def quack(self):
        print("Quaaaaaack!")
    def feathers(self):
        print("The duck has white and gray feathers.")

class Person:
    def quack(self):
        print("The person imitates a duck.")
    def feathers(self):
        print("The person takes a feather from the ground and shows it.")
    def name(self):
        print("John Smith")

def in_the_forest(duck):
    duck.quack()
    duck.feathers()

def game():
    donald = Duck()
    john = Person()
    in_the_forest(donald)
    in_the_forest(john)

game()
Comment

duck typing in python

class Person:
    def help(self):
        print("Heeeelp!")

class Duck:
    def help(self):
        print("Quaaaaaack!")

class SomethingElse:
    pass

def InTheForest(x):
    x.help()

donald = Duck()
john = Person()
who = SomethingElse()

for thing in [donald, john, who]:
    try:
        InTheForest(thing)
    except AttributeError:
        print 'Meeowww!'
Comment

duck typing in python

class Duck(object): 
   def quack(self): 
      print "Quack" 
 
class Mallard(object): 
    def quack(self): 
        print "Quack Quack" 
 
def shoot(bird): 
    bird.quack() 
 
for target in [Duck(), Mallard()]: 
   shoot(target)
Comment

PREVIOUS NEXT
Code Example
Python :: how to print multiple integers in python in different line 
Python :: [1,2,3,4,5] 
Python :: pandas get number unique values in column 
Python :: shebang line python 
Python :: ValueError: tuple.index(x): x not in tuple 
Python :: simple click counter in python 
Python :: sklearn impute 
Python :: wavelet transform in machine learning 
Python :: how to make an action repeat in python 
Python :: traduce query model 
Python :: How to install proxy pool in scrapy? 
Python :: space weather dashboard build your own custom dashboard to analyze and predict weather 
Python :: emacs pipenv not working 
Python :: python char to hex 
Python :: from wireframe GUI design to python tkinter 
Python :: how to update pip python 
Shell :: add-apt-repository command not found 
Shell :: stop apache server 
Shell :: postgres status ubuntu 
Shell :: installing zoom on ubuntu 20.04 
Shell :: conda install keras 
Shell :: remove docker container 
Shell :: install snap on kalicannot communicate with server: Post "http://localhost/v2/snaps/core": dial unix /run/snapd.socket: connect: no such file or directory 
Shell :: clear npm logs 
Shell :: installing java on linux 
Shell :: install ngrok ubuntu 20.04 
Shell :: how to update portainer 
Shell :: how to install pipenv on mac 
Shell :: clear dns cache 
Shell :: npm install legacy peer deps 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =