Lesson-16 | Enumeration Class | [OOP in Python]

preview_player
Показать описание
This is the lesson number 16 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 Enumeration Class. An enumeration class has members as the objects having names and values.

Link to complete playlist:

Lesson Code:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*Review Questions of the Lesson*
1- Values of more than one members can be same. Give one such example!

2- The value of a member can be a list. Suppose we have Subject enumeration class with member names as Subject short name and value will be a list containing Subject Code, Full Name and Credit Hours in a list. One example member is:
CP2=['MCT-243','Computer Programming-II',2]
What will be the best way to access subject code or name or credit hours? [Hint: Enumeration class members are objects like objects of regular class. And they can have attribute. Think of adding code, name and credit hours as property of object]

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

Try to understand the hint for Question 2. If s is a Subject Enumeration class variable, I want that s.code should return code of the subject and so on for s.subName and s.CH without accessing the list and index. Create getters using property decorator.

menaeem
Автор

2. As you said enumerate class members are objects, so if a list is the value of that member so we can simply access it by applying the list address of code or name or credit hours. For example, in that case, we can access the code of subject by simply that command:
print(Subject.CP2.value[0])

hamzamushtaqtoor
Автор

2. As the subject is an enumerate class and CP2 is an object of that class having its value as a list. So by simple index technique of list we can access all the attributes of the CP2 object. The code is as follows
Val=[Subject.CP2.value(i) for i in range(0, len(CP2)+1)]
Now the list 'Val' will have all the values of the CP2 object regardless of the length of the CP2 list. In this way we don't have to put in the indices by ourselves.

However if only one attribute is required, we can do the following
Subject.CP2.values(i) where i is the index of the attribute.

hamzarizwan
Автор

1) Bank Balance values of the members of the enumerate class can be the same.
2) In the getter method of subjects, return self._Sub.value[i], where i is the corresponding element of the list can be accessed by giving the element index to i.

ahmedimran
Автор

1)Height(value) of more than one person(members) in a group can be the same.
2)It can be accessed as "Subject.CP2.value[i]", where 'i' is the index of the required value.

shahwarsadaqat
Автор

We can access the subject code, name or credit hours by creating getters using a property decorator.
@property
def SubjectCode(self) :
return self.values[0]
and same for the name and credit hours with index 1 and 2.

We can access them as:

and it will print 2.

ridafatima
Автор

1. We can consider the example of class Student Subject Grade using Enumeration Class where defining Grades as values and student names as names forming members of the Enumeration class for a specific subject. Therefore, the values (Grades) can be same for different names(student names) for a specific subject.

rixer-music
Автор

2- we can access by having a getter function for code, name, and credit hours in enum class.
@property
def code(self):
return self.values[0]

@property
def name(self):
return self.values[1]


@property
def creadit_hours(self):
return self.values[2]

ayeshaaslam
Автор

2)By making a getter function
@property
Def credit_hours()
return self. value [2]
Similarly getters for full name and code can also be made by setting index 0 and 1.

arfanisar
Автор

1. Yes, Values can be the same for more than one member. For example, in months class, some months have the same value as 31 days and some have value 30 days. So these values are the same for some of the members of that class.

hamzamushtaqtoor
Автор

2. We can iterate the member in a for loop and then we can access code, name or credit hours or anything by the list index method as we will know the list address of each after for loop.

tech_n_play
Автор

1- Values for more than one member can be same as two keys like AssocProf or Assoc Prof or assocprof etc can have same value 'Associate Professor'. This type of thing is used in browsers for easy browsing. Also in dictionaries like english to urdu.

rananomi
Автор

2) we can access by
@property
def subcode(self):
return self._desig.value[0]
by adding this in Subject class
# main code
print(Subject.CP2.code)
and same for full name and credit hour

atifriaz
Автор

Q1) Bank Balance values of the members of the enumerate class can be the same.
Q2) Use the getter method of subjects, return self._Sub.value[i], i is the corresponding element of the list accessed by giving the element index to i.

hishamawan
Автор

2)This can be done by adding getters in the enumeration class. One example is given below:
let we have *Subject* as Enumeration class.
@property
def creditHours(self):
return self.value[2]

subject credit hours can be accessed as follow:
Subject.CP2.creditHours

danishbismillah
Автор

1- yes there are some cases, for example here if the seats are vaccant for "Asso prof" and "Lect", they could be the same. Moreover, in case of books there price can be same or such.

moadibnasir
Автор

1)Weight(value) of more than one person(members) in a group can be the same.
2)We can access it as "Subject.CP2.value[i]", where 'i' is the index of the required value.

zainzakir
Автор

if we are defining objects of book class we can give them an attribute as genre ( which is an member of enumeration class of genre) we can add age restriction as values of the members, which can be same.

ahmadhussain
Автор

2. The code for the solution of this task is:

### Classes Code ###
from enum import Enum
class Subject(Enum):
CP2=['MCT-243', 'Computer Programming-II', 2]
class Instructor:
def __init__(self, fName, lName, subj):
self.fName=fName
self.lName=lName
self.subj=subj
self.courses=[]
def __repr__(self):
return f'{self.fName} {self.lName} - {self.desig}'
@property
def subj(self):
return self._subj.value
@subj.setter
def subj(self, newsubj):
if(isinstance(newsubj, Subject)):
newsubj=newsubj.name
if(newsubj in Subject.__members__):
self._subj=Subject[newsubj]
else:
raise ValueError(f'{newsubj} is not a valid Designation!')


Now, the best way to access subject code, name or credit hours is to write "[0]", "[1]" or "[2]" respectively in the print statement in main program, e.g. if we want to access subject name, main program will be:


### Main Program ###
i1=Instructor('Ahsan', 'Naeem', Subject.CP2)
print(i1.subj[1])


"Computer Programming-II" will be printed now. If we want to access subject code, main program will be:


### Main Program ###
i1=Instructor('Ahsan', 'Naeem', Subject.CP2)
print(i1.subj[0])


It will print "MCT-243".

muhammadfaseehbinnaeem
Автор

1. yes, can be same for :enumerate class of books title as book name and and writer name as values

muhammadammar
visit shbcf.ru