Python Tutorial for Beginners 33 - Python Composition

preview_player
Показать описание
In this Python Tutorial for Beginners video I am going to show How to use Class Composition in Python. Composition allows us to delegate some responsibility from one class to another class.
In Class Composition one class acts like a Container and other acts like a content. Composition represents "part-of" relationship.When there is a composition between two Python classes , the content object cannot exist without the container object.

#PythonTutorialforBeginners #ProgrammingKnowledge #LearnPython #PythonCourse
★★★Top Online Courses From ProgrammingKnowledge ★★★

★★★ Online Courses to learn ★★★

★★★ Follow ★★★

DISCLAIMER: This video and description contains affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This help support the channel and allows us to continue to make videos like this. Thank you for the support!
Рекомендации по теме
Комментарии
Автор

thx man! simple and short example. Finally got what is composition !

RECHDIful
Автор

This was helpful, clear and short - exactly what I needed. Thanks!

victorfleuriau-chateau
Автор

Love how thorough you are with explaining the concept with all the proper vocabulary words -- thank you so much!!!! better than my professors :)

michaeljagdharry
Автор

Great video. One suggestion: try naming your variables more clearly. For example, "pay" is incredibly vague. A better name for it in this class would be "monthly_pay". And "bonus" could be "annual_bonus". Good code should not require other developers to read through the code to understand a variable's meaning; rather it should be clear from the name of the variable, ideally.

RedShipsofSpainAgain
Автор

The problem with this approach is, that now you have a tightly coupled dependency between Employee and Salary. Suppose, that you need two types of salary. One being FullTimeSalary and other being ContractorSalary. Now you need complex logic in the initializor of the Employee class to choose which one to set to self.obj_salary. You should use dependency injection, where you pass the object of the ***Salary directly in the parameters if the initializor. This way you now have a loosely coupled dependency and dont pollute your class logic with unnecessary if statements.

KlemensSoftware
Автор

some things are just so simple it feels like the focus on it as a technique is akin to some sort of trick question. you just used an object of another class inside your new class. that's it. it feels like there just _has_ to be more to it than that.

Rin-qjzt
Автор

Great lecturing! Very clear and well thought out examples!

LenCedeno
Автор

Great guide, clear examples and speech

PaTZi
Автор

Clear and concise. Amazing explanation! Thanks a lot.

josswe
Автор

That is what I was looking for. Thanks! Easy to follow and understand.

muddassirghoorun
Автор

Your voice is so pleasing to the ears.

eagleeye
Автор

Thank you so much
Also can u explain delegation concept

debojitmandal
Автор

Thank you Sir, great video, clearly communicated and i am thankful for your time and effort.

TheTimtimtimtam
Автор

Wow.... I have been struggling with super() for a long time while trying to pass some information from one class to another. Somehow in the last year of tutorials/books/etc., composition was never mentioned. I had no idea this was a thing, and it is EXACTLY what I was trying to do with super(). Thank you very much, your videos are incredibly helpful!

dadsmight
Автор

respect, very useful, thanks so much!

muhammadmustafoomonov
Автор

Thanks for the explanation. Kudos for the clear and concise information

aplifiedchaos
Автор

What if we have a list in the empolyee class and that list must have the attributes of another class(salary)??

ramasaranyachatrathi
Автор

Thanks sir. Please, why have you used obj_salary statement while we have annual salary as a method? Thanks

mahamanoumar
Автор

Why does the print function under total_salary method not print annual_salary?
class Salary:
def __init__(self, pay, bonus):
self.pay=pay
self.bonus=bonus
def annual_salary(self):
return (self.pay*12) + self.bonus

class Employee:
def __init__(self, name, age, pay, bonus):
self.name=name
self.age=age
self.obj_salary=Salary(pay, bonus)
def total_salary(self):
print

emp=Employee('max', 32, 15000, 10000)

aries
Автор

How does it differ from Aggregation? I can create aggregation here by creating a Salary object outside the class and pass it to the Employee's constructor, right?

gowthamsankaran