Lesson-11 | Polymorphism & Magic Methods | [OOP in Python]

preview_player
Показать описание
This is the lesson number 11 of the lesson series on Object Oriented Programming (OOP) in Python. It will be a complete course with the aim is to cover almost everything needed in Object Oriented Programming.

We will cover an important topic of Polymorphism where one operator has multiple behaviors depending upon the type of the object on which that is being applied. For example, + operator behaves differently when applied on number than when applied on strings.
We will see that we can define the support of different operators for our class objects using different special or magic methods.

Link to complete playlist:

Lesson Code:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*Review Questions of the Lesson*
1- With the first definition of __add__ method, where the results was list of subjects of both the students, if we execute std1+std2+std3, will it be a list of all three students? Think of it without executing the statement and then verify.

2- With the second definition of __add__ method, where both students become groupMemmber of each other, if we execute std1+std2+std3, it must generate the error as three students cannot be the group members. So are you getting this expected error?

#OOP #ObjectOrientedProgramming #Python
Рекомендации по теме
Комментарии
Автор

1. _add_ works in parts, it takes first two arguments and sum them, and then adds sum of the former two with the next argument. In this case, the sum of first two arguments will be a list while the third one is an object which can not be concatenated with list, hence error is raised

SafiUllah-kgku
Автор

2: we will get the type error as this code wont return anything on setting the group members. so none + std3 will give error.

ShehzadKhan-ojer
Автор

1. __add__ works in parts, it takes first two arguments and sum them, and then adds sum of the former two with the next argument. In this case, the sum of first two arguments will be a list while the third one is an object which can not be concatenated with list, hence error is raised.

hadiqarabnawaz
Автор

2. In this case also the same error "unsupported operand type(s) for +: 'NoneType' and 'Student' . We can add std1+std2 and std1+std3 both are the group members and it gives result but std1+std2+std3 gives error and we can't add none in other object as "+" operator method start with zero integer and it does know how to add student instance

faqeehakhan
Автор

1. Sir I think without execution that this will generate some but don't know which type of error but after execution I see that it generates type error because we are only giving two inputs in our function so therefore it generates such error.

hamzamushtaqtoor
Автор

2- Yes Sir! TypeError: unsupported operand type(s) for +: 'NoneType' and 'Student'

ProphetPearls
Автор

1-No, it will generate error as first two students were treated as the input argument and will return the list while due to two input arguments, the third student(std3) will be treated as an object and we cannot add a list to an object.

moadibnasir
Автор

1: when they are added, it will form a list containing the subjects of std1 and std2 and when std3 is added into that list an error will be generated as std3 is type 'object' and can't be added in list.

ibtisamhassan
Автор

2-We are not getting the expected error. This add method is not returning anything for setting members. So none + std3 will give the "Type error".

shahrozahmed
Автор

1- No, when this command is executed, std1+std2 will be added as per __eq__ method, and in turn yields a list of courses, but when its time to add std3 to the list, of course an error will be generated because they are objects of diferent classes. So as a result, TypeError will be produced.

maryumkainat
Автор

1-no, it will generate type error because when it adds std1+std2 it gives a list. list and object can't be concatenated.

ayeshaaslam
Автор

1. If std3 contains the list of subjects instead of being the object, the output of this will be a list. But for now it generates an error because a list cannot be added to an object

muhammadalihadir
Автор

1) Std1 and Std2 are list by __add and when we add std3 in it it will give error .as std3 is a object

waleedscapture
Автор

1) If we add std1+std2+std3, it will create an expected error that List cannot be added to an object. and verified later when running the program.

Ibrifs
Автор

2- TypeError: unsupported operand type(s) will be generated because when we add std1 and std2 it returns 'None' and when 'None' is added with std3 it generates error.

ridafatima
Автор

1) Since the condition of 2 students to be made as group members is used in the attribute so by adding the 3rd student will definitely generate an error, as the making three students member of each other at a time is not introduced in the attribute.
2) Yes, the error(Type error) is generated.

ahmedimran
Автор

2- Yes, if we execute std1+std2+std3 then it will generate an error that as: unsupported operand type(s) for +: 'NoneType' and 'Student'.
It is because this __add__ method isn't returning anything and thus none type cannot be concatenated with some type.

rananomi
Автор

2- Yes, TypeError will be generated because when this command is executed, std1+std2 returns None and then when std3 is added to it, of course due to difference in their types, an error will be produced.

maryumkainat
Автор

2. Yes, because when we add std1+std2+std3, std1+std2 execute and returns answer but when we add std3 in above answer, error will generate as None and std3 cannot become groupmembers.

syedatayyababukhari
Автор

1) It will generate TypeError: As Std1 being Added with Std2 generate a type"list" that cannot be further added with std3 type"object"

khaqanahmed
join shbcf.ru