Python duck typing 🦆

Опубликовано: 10 Январь 2025
на канале: Bro Code
23,459
1.3k

Python duck typing tutorial example explained

#python #duck #typing

duck typing = concept where the class of an object is less important than the methods/attributes
class type is not checked if minimum methods/attributes are present
“If it walks like a duck, and it quacks like a duck, then it must be a duck.”

class Duck:

def walk(self):
print("This duck is walking")

def talk(self):
print("This duck is qwuacking")

class Chicken:

def walk(self):
print("This chicken is walking")

def talk(self):
print("This chicken is clucking")

class Person():

def catch(self, duck):
duck.walk()
duck.talk()
print("You caught the critter!")


duck = Duck()
chicken = Chicken()
person = Person()

person.catch(chicken)

Bro Code merch store 👟 :
===========================================================
https://teespring.com/stores/bro-code-5
===========================================================

music credits 🎼 :
===========================================================
Up In My Jam (All Of A Sudden) by - Kubbi   / kubbi  
Creative Commons — Attribution-ShareAlike 3.0 Unported— CC BY-SA 3.0
Free Download / Stream: http://bit.ly/2JnDfCE
Music promoted by Audio Library    • Up In My Jam (All Of A Sudden) – Kubb...  
===========================================================