Lesson-03 | Class Level Attributes | [OOP in Python]

preview_player
Показать описание
This is the lesson number 03 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 talk about the class level attributes. We will see how different objects or instances can access class level attributes.

Link to complete playlist:

Lesson Code:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*Review Questions of the Lesson*

1. At 01:10 , department attribute is accessed for two student objects and is returned correctly. Can we access it for class rather than a specific object? How will we do that?
2. If subject MOM is repeated in the original offSubjects list as:
offSubjects=['Mech','LA','ES','CP-II','MOM', 'MOM','Proj']
At 04:35 will MOM be added two time?

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

1. Yes, we can access it for class rather than a specific object. We will do it by using "Student.department".
2. At 04:35, MOM will not be added two time because of the following condition we used in "registerSubject" function:
"s not in self.courses"

muhammadfaseehbinnaeem
Автор

I know I need to follow the correct sequence, but my current task forces me to learn from oop first.
Everytime I learn a lesson from you, it will quickly become another real-world solution tomorrow.
Thank you very much!

rotrose
Автор

Nice lecture as usual...keep on uploading more...thank u...

Astromoola
Автор

1- department is class attribute (which is accessible to every object and class ).
yes it can be access for class as well
class student:
department="mecha"

print(student.department) ### output : mecha

2-If subject MOM is repeated in the original offSubjects list as:
offSubjects=['Mech', 'LA', 'ES', 'CP-II', 'MOM', 'MOM', 'Proj']
it will not added two time to list of registered_subjects because we have put condition (not to add already existing subject).
Due to which MOM will be added once.

ayeshaaslam
Автор

*Review Questions of the lesson:*
1. At 01:10, department attribute is accessed for two student objects and is returned correctly. Can we access it for class rather than a specific object? How will we do that?
2. If subject MOM is repeated in the original *offSubjects* list as:
offSubjects=['Mech', 'LA', 'ES', 'CP-II', 'MOM', 'MOM', 'Proj']
At 04:35 will *MOM* be added two time?

LearningOrbis
Автор

1- Yes, we can access it as student.department for class. Actually, department is a class-level attribute and those attributes which are class level can be accessed in both ways, i.e. by class or by objects.

2- It will be added only once due to the if condition used in the for loop block. i.e,
for s in in sub:
if s in student.offsubjects and not in self.courses

abdurrehmansarwar
Автор

q1. Absolutely, we can access it for a class as well as for any instance because it is a class
level attribute. This is done by using 'Student.department'.

q2. No, MOM will not be added twice because of the condition inside the for loop(s not in self.courses).

hamidiftikhar
Автор

1)It can be accessed by the "Student. department" because it is a class-level attribute.
2) The subject MOM can not be repeated twice because the condition says if the subject is already added to the list then it will not append for the second time.

shahwarsadaqat
Автор

1) Yes, department for class can also be accessed by simply writing "print(Student.department)".
2) No, MOM will not be added twice as we have restricted it by the condition that a subject will only be added to reg subjects list if it isn't already present in it.

arfanisar
Автор

1- Yes, it is accessible via 'Student.department'.
2- No, it won't get added twice because the second 'if-and' condition prevents that from happening.

noorulain
Автор

...
1) Yes, department for class can also be accessed by simply writing "print(Student.department)".
2) No, MOM will not be added twice as we have restricted it by the condition that a subject will only be added to reg subjects list if it isn't already present in it.

zainulhassan
Автор

1_Department is a class method so it can be accesed without creating an instance.
2_ it will not be added because of condition used in for loop.

ahmadiftikhar
Автор

1)as class level attributes are accessible by any instance of class so department can be accessed by any instance and method of class. i.e Student.department
2) No, MOM will not be repeated in the original list because condition is applied on reg subject that will not allow repetition

hafizaqib
Автор

1-Yes, we can use department attribute for class rather than a specific object . We can access it by "Student.department"
2-No, MOM will not be added two times, it will be added once and repetition of it will be discarded in the output because of the condition present in register subject function i.e., 's not in self.courses'

rajahaseebahmad
Автор

1.Yes we can access the department for any instance/object by Student.department as its a class level attribute.
2.No, MOM will not be added twice because of the condition used in registerSubject() method.

wasifalii
Автор

1. Yes, an class attribute can also be accessed as a class. It can be done using Student.department
2. It will not be added twice because of if statement that filters out the possibility

muhammadalihadir
Автор

1. of course we can add it as a class attribute
2. in the absence of condition it would be two times but not in this situation

lostdiscoverer
Автор

yes, by using Student.department
no it won't be added twice because we've added an if condition that a subject can be registered only ONCE for a student.

hadiqarabnawaz
Автор

1) Yes we can access it because "department" is a class attribute.
2) It will not be added two times because we used the "if" condition for repetition.

simplelife
Автор

1) yes we can do it by making it class attribute and call it as
Student.department

aurangzaibvirk