Lesson-15 | Multiple Classes | [OOP in Python]

preview_player
Показать описание
This is the lesson number 15 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.

In this lesson we will see concept of Multiple Classes and will create link between attributes of the classes.

Why should not we set the default value of an input argument as empty list:

Link to complete playlist:

Lesson Code:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*Review Questions of the Lesson*
1- Describe in simple words why should not we set the default value of an input argument as Empty List

2- How can we make the list of books for an author be in order of publication year such that whenever a book is created and is added into the books list of author(s), the list must be in descending order of publication year.

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

1)The list is a mutable data type such that the list is updated whenever the function is called. Thus, a better choice is to use NONE rather than an empty list.
2) This can be done by the first sortation books, then adding them:
a.books.sort(key=lambda x:x.year, reverse=True)

irsaidress
Автор

1.when a function us defined, a new list is created and same is used in every successive call, python default arguments are evaluated once (when a function is defined) not each time the function is called.So the better way is to use none instead of []
2.After line 25, we can add something like:
a.books.sort (key=lambda b:b.year, reverse=True)

samrayousaf
Автор

1. List is mutable which means it can be modified. when we set its default value as input argument, the function will keep using its default value in every call. Because the default parameter values are always evaluated when the 'def' statement of that fucntion is executed.

tech_n_play
Автор

q#1: Actually, a new list is created whenever a function is defined and the same list is available when we call it in function so it is better to set it as None
q#2: a.books.sort(key=lambda self: self.year, reverse=True)

hamidiftikhar
Автор

The logic behind review question no is very simple. As we have studied in CP1 that the purpose of list is to evaluate results once when the function is defined, not every time when function is called. This is the reason that we do not set default value of an input arguments as empty list

hafizasadullah
Автор

1) Whenever a function is created a new list is created and the when the function is called the same list is used, so it is advisable to set its default value as None.
2) We can use sort for sorting list and Key to sort it order of year.
a.books.sort(key=year, reverse=True)

waleedraza
Автор

1: List is a mutable datatype so when we call a function, the list get updated every time so it is good to use none.
2: a.books.sort(key=lambda x:x.year, reverse=True)

arslanrafiq
Автор

1.List is a mutable datatype so the list get updated every time when function is being called. So, a better choice is to use NONE instead of an empty list.
2) This can be done by 1st sorting books and then appending them:
a.books.sort(key=lambda x:x.year, reverse=True)

maeshaijaz
Автор

1. A list is a mutable data type which can be updated with every new value. If we set list as a default data type in a user defined function the function will take previously updated value if function is already executed so we should set the list equal to none to take new updated value.

bhattimentor
Автор

1) List is a mutable datatype so when we call a function, the list get updated every time so it is good to use none.

2) a.books.sort(key=lambda x:x.year, reverse=True)

abdulrehmansajid
Автор

1. Empty list is a mutable datatype, when function is called first time, new empty list will be created. But when function is called again, previous list will be assigned in place empty list. It is a kind of repetition in which list id keeping its value inside the function.

abdul_majid.
Автор

1: None will be preferred as list is mutable when the function will call it will update the previous list and hence new elements will added along with the previous ones.
2: a.books.sort(key=lambda x:x.year, reverse=True)

nukhbaiqbal
Автор

1.Whenever we call function, the list will be created after every call, it will use the previous list and will always get updated so it is better to set the value 'None'.
2.By this method
a.books.sort(key=year, reverse=True)

abdulrehmankhalid
Автор

1: We do not use empty list because every time we call a function it will terminate the list into empty list so to overcome this thing we use 'none' instead of empty list.
2: a.books.sort(key=lambda x:x.year, reverse=True)

saifullahafzal
Автор

1)List is a mutable data type so when we call a function, the list gets updated every time so it is good to use none
2)a.books.sort(key=lambda x:x.year, reverse=True)

AliIrfan-soxf
Автор

1)with new function, new list is created so instead of updating it with multiple functions call, we should use none in input argument
2)a.books.sort(key=year, reverse=True)

dawood
Автор

1. List being a mutable datatype will be updated as an empty list whenever we call function so we prefer to use 'none'.

2.
a.books.sort(key=lambda x: x.year, reverse=True)

rajahaseebahmad
Автор

2- It is done directly by applying sort function after a.books.append(self)
list has been appended as:
a.books.sort(reverse=True, key=lambda self : self.year)

noorulain
Автор

2) Inside the setter method for authors we have to add another line after appending the book in author's list of books to arrange the books according to descending order of publication year.

a.books.sort(key=lambda self:self.year, reverse=True )

muhammadasaad
Автор

Rev-1)
Because every time we call a function is called it will make the list empty list.
Rev-2)
we can use sort function as follows
a.books.sort(key=lambda x:x.year, reverse=True)

AliHamza-zwvt