Classes in Python

Python Programming More of Python
16 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.94
List Price:  €92.78
You save:  €27.83
£55.57
List Price:  £79.40
You save:  £23.82
CA$95.27
List Price:  CA$136.11
You save:  CA$40.83
A$107.88
List Price:  A$154.13
You save:  A$46.24
S$94.55
List Price:  S$135.08
You save:  S$40.52
HK$547.57
List Price:  HK$782.28
You save:  HK$234.71
CHF 63.42
List Price:  CHF 90.61
You save:  CHF 27.18
NOK kr759.63
List Price:  NOK kr1,085.23
You save:  NOK kr325.60
DKK kr484.38
List Price:  DKK kr692.01
You save:  DKK kr207.62
NZ$117.46
List Price:  NZ$167.80
You save:  NZ$50.34
د.إ257.02
List Price:  د.إ367.19
You save:  د.إ110.16
৳7,682.93
List Price:  ৳10,976.08
You save:  ৳3,293.15
₹5,837.41
List Price:  ₹8,339.52
You save:  ₹2,502.10
RM331.26
List Price:  RM473.25
You save:  RM141.99
₦99,285.71
List Price:  ₦141,842.81
You save:  ₦42,557.10
₨19,466.20
List Price:  ₨27,810.04
You save:  ₨8,343.84
฿2,553.28
List Price:  ฿3,647.70
You save:  ฿1,094.41
₺2,262.39
List Price:  ₺3,232.12
You save:  ₺969.73
B$349.43
List Price:  B$499.21
You save:  B$149.77
R1,335.92
List Price:  R1,908.54
You save:  R572.62
Лв126.45
List Price:  Лв180.65
You save:  Лв54.20
₩94,634.34
List Price:  ₩135,197.71
You save:  ₩40,563.37
₪258.03
List Price:  ₪368.63
You save:  ₪110.60
₱3,943.57
List Price:  ₱5,633.91
You save:  ₱1,690.34
¥10,600.67
List Price:  ¥15,144.47
You save:  ¥4,543.79
MX$1,161.53
List Price:  MX$1,659.40
You save:  MX$497.87
QR255
List Price:  QR364.31
You save:  QR109.30
P959.59
List Price:  P1,370.91
You save:  P411.31
KSh9,203.68
List Price:  KSh13,148.68
You save:  KSh3,945
E£3,310.52
List Price:  E£4,729.52
You save:  E£1,419
ብር3,976.27
List Price:  ብር5,680.63
You save:  ብር1,704.36
Kz58,526.41
List Price:  Kz83,612.74
You save:  Kz25,086.33
CLP$68,581.80
List Price:  CLP$97,978.20
You save:  CLP$29,396.40
CN¥506.04
List Price:  CN¥722.95
You save:  CN¥216.90
RD$4,144.87
List Price:  RD$5,921.50
You save:  RD$1,776.62
DA9,443.17
List Price:  DA13,490.83
You save:  DA4,047.65
FJ$158.28
List Price:  FJ$226.12
You save:  FJ$67.84
Q545.87
List Price:  Q779.86
You save:  Q233.98
GY$14,645.83
List Price:  GY$20,923.51
You save:  GY$6,277.68
ISK kr9,762.20
List Price:  ISK kr13,946.60
You save:  ISK kr4,184.40
DH709.20
List Price:  DH1,013.19
You save:  DH303.99
L1,234.28
List Price:  L1,763.34
You save:  L529.05
ден3,991.30
List Price:  ден5,702.11
You save:  ден1,710.80
MOP$564.10
List Price:  MOP$805.89
You save:  MOP$241.79
N$1,325.35
List Price:  N$1,893.44
You save:  N$568.09
C$2,576.69
List Price:  C$3,681.15
You save:  C$1,104.45
रु9,334.54
List Price:  रु13,335.63
You save:  रु4,001.09
S/259.58
List Price:  S/370.84
You save:  S/111.26
K267.89
List Price:  K382.72
You save:  K114.82
SAR262.49
List Price:  SAR375
You save:  SAR112.51
ZK1,765.85
List Price:  ZK2,522.76
You save:  ZK756.90
L322.98
List Price:  L461.43
You save:  L138.44
Kč1,645.46
List Price:  Kč2,350.75
You save:  Kč705.29
Ft25,709.21
List Price:  Ft36,729.02
You save:  Ft11,019.81
SEK kr749.87
List Price:  SEK kr1,071.30
You save:  SEK kr321.42
ARS$60,034.20
List Price:  ARS$85,766.82
You save:  ARS$25,732.62
Bs483.70
List Price:  Bs691.04
You save:  Bs207.33
COP$271,296.95
List Price:  COP$387,583.68
You save:  COP$116,286.73
₡35,581.11
List Price:  ₡50,832.34
You save:  ₡15,251.22
L1,728.07
List Price:  L2,468.78
You save:  L740.71
₲516,441.87
List Price:  ₲737,805.73
You save:  ₲221,363.85
$U2,647.22
List Price:  $U3,781.90
You save:  $U1,134.68
zł280.50
List Price:  zł400.73
You save:  zł120.23
Already have an account? Log In

Transcript

So in this lesson, we'll deal with the classes. So when we talk about classes, we talk about object oriented programming. To understand it better, let me give you an example. So imagine we have to create a game where we have to put in some characters see thousand characters. So in order to do this as quickly as possible, we should first define the coma attributes there. So therefore, the moments in the various direction, the energy bar, some specific actions, and so on.

Basically, we define a series of karma features. So we can put them in a class in order to use them for a free character without having to do or without having to go to set all the parameter one at a time manually. So with an example, we can start getting exactly why using classes become really useful. So Let us first show you how to define a class. So, proceed with this. So, to define a class, we need to first write class and then I have to give an alias over here or you can say we can give it a name for example person.

So, person is the name of the class. Now, here I have to give up coolant. So, once this is done, we should think up the common attributes we have to set for each character. So, let me give you an example on this one. First of all, that's a sign of value of 1000 for their energy bar. So, your I assign energy 2000 So, here I will write energy equal 2000 and after that we define a function that actually shows the status of the energy in Real time like this, so here I'll write def.

Then display underscore energy. Now here, write self. And again Eva colon so so after that, I will ask the program to print it. So print here I'm right. Energy status is this and then after this give a comma and write parser dot energy. So here we must recall the energy variable as we saw.

So in it in this case is a part of a person and in order to recall it We have to write person dot energy. So this is why here I've written person dot energy. Now, why is the self parameter we wrote in the line before is actually a standard parameter that is this one. This self parameter is a standard parameter that is used to show the character condition in real time. So now going on forward, we have to do a defined function that shows the name of the character. So again, with the self parameter, here I'll write def.

Then display underscore alias. And then within bracket right self, again, here diva colon. And next what I have to do is I have to print it. So he I'll write, print, then self dot aeleus Okay. Now, let's set the init function here. So this function is the initialization function and we are going to put between the brackets.

So here after energy equal to initializing energy equal to thousand, here, I will initialize the init function or define the init function. So def here, this is how we'll write it. Then, within presses write the parameters self, le, s, and H. So here I've used three parameters self, alias, and age. So here I've written three parameters. Now if you use more This free parameter you have to define it over here. So, here we have to give a column.

Now come to the next slide. So, this function is also called the builder function, that is the function that every time of the class is called. In this case person, it recalls it automatically. So inside it, we are going to give the alias to self dot alias in this way. So here I write, self dot alias and then I'll write equal to alias. Then again, here we'll do the same thing for each.

So self dot age is equal to age Okay, so the initial search function and the self parameter are the elements of the class of this kind. So the latter would coat itself because it is usually called like this by convention, but we can't call it as we want. The important thing is to have parameters that shows the current situation. So at this point, we can run this process. So we'll run this program here. So it's still running, and it ran perfectly without an error.

So I can say that my class is created perfectly. So now let's create two characters. So to create the character here, I'll write p underscore one which is equal to Now I have to write person and within this person person's the class name. So here I have to mention that on which class I'm working. So I've written person and within this first bracket, say, I have to write a name. So I am Right, Sam, and Eva comma, and I have to give its age.

So let's say I'll give its age to 24. Okay, now, let me create one more character. So P two is the second character. So here I am again right person, and within this I'll write Mac, then say it is 26. Now, in the initialization function, we have defined three parameters. But in this case, self is automatically excluded because it becomes the standard parameter of the class.

Then we directly write the earliest one like the this one. So I've just mentioned the alias and the age. And this is the name of the character and the age as I've written. So now let's run this program. So it ran perfectly. So let me check that the value that we have incorporated whether it is created correctly or not no so to do that and just print p underscore one dot alias, so this run it and see it printed Sam so P wha, the alias that is the name is printed as Sam now, if I have to check for the second one, so P two dot h so just print it and see it printed 26 so it is created perfectly.

Okay, remember one thing here, if you don't mention to underscore and then init function and then again to underscore your program will not run correctly. So remember here I've used to underscore to underscore before and to underscore after init function now once we have created our character through class Let's use the to function we have defined in the class itself that is display so display underscore energy and display underscore areas. So first let's write p underscore one dot display, underscore energy. Now put a bracket and just run it see what it gives, it gives energy status is thousand. Now check in state. If we can add it to the areas we previously gave to the to character.

For example, let's write p underscore one dot and yours. So here we are adding one and yes to the class Previously, we have Added to, to alias that is two persons that is Sam and Mac now Here I'm adding one new. So let's just write it like this p underscore one dot alias and then within this write the name. So here I'm writing john and now just print it and run. So now john is created. So what it does is is edited the P one alias.

So first it was Sam and now it was john. So this process is called edit method. Now in addition to this edit method, we can also delete so to delete element we have to right there, then write p underscore one and just write dot alias and now p underscore one dot LDS. So just run it. See it, give it up. Because it first deleted the item deleted p one dot LDS and then I'm printing p dot areas from where it will get P dot LDS it cannot get because it is already deleted.

Now if you don't understand I'll show you in this way. So first again I'll create this one I'll run this one and now p one.le s is Sam now Now I'm not print it first I'll run this so it run without an error See, and now again, I'm calling it all I Oregon Sam printing p dot alias. Now, if I print it, it should give me error because there is nothing called P and score one dot areas because I have already deleted it. So if I now want to print it from where Python will get it will show me an error because C C data person object has no attribute areas, okay? So it is now deleted in the previous step. Now, if I Want to print it from where it will get the value it will not get the value and it will always show you an error.

So one more method that we'll use today is the has attribute. So let's write the has attribute. So it has a shorter form, we don't write has attribute like that. So we just write has a term that is he is a TT er. Now within this give a first pair of first bracket and within the pair of as bracket i p underscore one comma within bracket, right age and run. So it's printing true now why the splintering two because for p one ages there I haven't deleted h I have only deleted the alias.

So age is already there. So it is printing true because H is there it is present. Now instead of age if I just change that, if he seems the attribute of again if I just right suppose this be on underscore one and then give a comma write alias. Now see what it will print, it will print false, because I've deleted p underscore n yes over here it is not present. So, it has printed false. So, the next attribute that we are going to use the get attribute.

So, how to use it it is it has also a shorter form. So, get attached This is and within this just write p underscore one comma H, if you just run it, what it will give it will show you the value of p underscore one h gate attribute means, it will show you the value what is the value of that particular element so, it will just show now I if I write this p underscore two and then write an alias what it will print it will print Mac. So, the next attribute that we are going to use is the set attribute set attribute is used to change the way Suppose here I use, again the shorter form p underscore one. So previously I've seen that p underscore one, h is the value is 24. Now here I'll write p underscore one H. And now I want to change the value, change the value to 30.

So I've written 30 over here, so give provide the new value that you want to write over here. And now run it okay. And now print this. So I'll copy this get from here, and I'll just paste it here. Okay. So now just run it and see see the new value.

See, when you value is 30. Now, it has changed from 24 to 30. So get attribute will change its value of a particular element. Now the last attribute that we are going to use is the Delete. So again, the shorter form they'll they'll attend Then within this first bracket write p underscore one, then comma then each. So this age will be deleted.

Okay? So it's deleted. Now, if you rerun this program, it will show you an error because it is already deleted it there is no 18 p one. So, run it, it will show you an error, because nothing to delete, it will show you an error because it is already deleted and you are running this program twice. So, it will show you an error. So, we have come to the end of this lesson of this class, we have learned how to create a class and how to use different functions, how to use different attributes.

So, we have learned this has attribute data attribute set attributes delete attributes. So, by the end of this lesson, there are three main question to which we must be able to answer. The first is what are they for? The answer is that defining Class mainly serves to simplify repetitive tasks, defining common attributes in order to use them. So by us giving a common attributes, we can use them. The second question is, how can a class be defined?

Now let's remember that a class must at least have an initialization function or also called init function with a standard parameter that we usually call self in the third and the last question is how can we use it? So to use the classes we have to assign a variable, the name of the class and any attributes we want inside of it. So so this is the end of this lesson. So till then keep practicing. See you in the next video. Thank you.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.