Learn Python POLYMORPHISM in 8 minutes! 🎭

preview_player
Показать описание
# Polymorphism = Greek word that means to "have many forms or faces"
# Poly = Many
# Morphe = Form

# TWO WAYS TO ACHIEVE POLYMORPHISM
# 1. Inheritance = An object could be treated of the same type as a parent class
# 2. "Duck typing" = Object must have necessary attributes/methods

from abc import ABC, abstractmethod

class Shape(ABC):

@abstractmethod
def area(self):
pass

class Circle(Shape):
def __init__(self, radius):

def area(self):

class Square(Shape):
def __init__(self, side):

def area(self):

class Triangle(Shape):
def __init__(self, base, height):

def area(self):

class Pizza(Circle):
def __init__(self, topping, radius):
super().__init__(radius)

shapes = [Circle(4), Square(5), Triangle(6, 7), Pizza("pepperoni", 15)]

for shape in shapes:
Рекомендации по теме
Комментарии
Автор

# Polymorphism = Greek word that means to "have many forms or faces"
# Poly = Many
# Morphe = Form

# TWO WAYS TO ACHIEVE POLYMORPHISM
# 1. Inheritance = An object could be treated of the same type as a parent class
# 2. "Duck typing" = Object must have necessary attributes/methods

from abc import ABC, abstractmethod

class Shape(ABC):

@abstractmethod
def area(self):
pass

class Circle(Shape):
def __init__(self, radius):
self.radius = radius

def area(self):
return 3.14 * self.radius ** 2

class Square(Shape):
def __init__(self, side):
self.side = side

def area(self):
return self.side ** 2

class Triangle(Shape):
def __init__(self, base, height):
self.base = base
self.height = height

def area(self):
return self.base * self.height * 0.5

class Pizza(Circle):
def __init__(self, topping, radius):
super().__init__(radius)
self.topping = topping

shapes = [Circle(4), Square(5), Triangle(6, 7), Pizza("pepperoni", 15)]

for shape in shapes:
print(f"{shape.area()}cm²")

BroCodez
Автор

Thank you, Bro! I'm from Brazil and I'm learning a lot about Python with your channel, your content is fantastic!!!!

gbernardo
Автор

Variables dance, loops entwine,
Functions called, their tasks align,
Conditions branch, decisions made,
In this digital serenade.

Errors lurk in shadows deep,
Bugs that make the weary weep,
But with each struggle, wisdom grows,
And through persistence, knowledge flows.

Debugging trails like whispered clues,
In forests dense, where logic brews,
Step by step, the journey’s charted,
Until at last, the code’s imparted.

Arrays align, and classes sing,
Inheritance and polymorphism bring,
A harmony of structured might,
In this symphony of code’s delight.

Deploy the build, let it be free,
Across the world, from sea to sea,
Users engage, a dream fulfilled,
A coder’s vision, skilled and willed.

Yuiiiiu
Автор

This guy is the god of code. He does a tutorial almost every single day

Ertoko
Автор

Bro Code, more like God Code. These explanations are amazing. Whenever I need something cut down to digestable python form, I know where to go

bola
Автор

I'm a simple man: I see like - I leave a Bro. Well, you got the point...

Thanks for the best lessons one can possibly find

joehaar
Автор

bro please continue the react course :(😕

William-dtmr
Автор

Thank you! Great video (as usual).
: )

piano_depois_dos_
Автор

I don't understand crap help me fr

Sunford_
Автор

please make video on encapsulation, abstraction, class method, static method and instance method.
looking forward to see the videos this week .
Thank you

ChukwuemekaAmblessedchinenye
Автор

Bro please make a series's of Data Structure and Algorithms with JavaScript

malikgulraiz
Автор

LONDON PARIS AMSTERDAM. YEAH IM OVER SEAS

myster
Автор

hi bro i've struggle to learn other programming languages cuz i started by python and i want to be SE help me please

ebatilaye