Hello guys and welcome to Python programming
tutorials by Amuls Academy. In the previous tutorial we discussed about
the property decorator and today also we are continuing the discussion on that. So in the previous tutorial we discussed about
how we can use class method as attributes using property decorator so today we will
see how to replace setter and getter and use property decorator. so alright first I’ll take a class because
as i said this property decorators are used on the class so I’ll take different example
so class name will be same that is Student, it will take one attribute called marks, so
i will initialise the marks self dot marks equal to marks. Next here i will take another method, this
method will calculate the percentage of that marks ok, so it will return. so here i am taking the total marks as 600
so i will divide the given marks by 600 and then i will multiply 100 to get the percentage
ok so here we are taking the six subject each of 100 marks that’s why total is 600 fine. Next this is my class which contains two methods
one is the initialisation method and another one is the percentage method and marks is
the attribute and this class calculates the percentage of given marks ok. so now i will
create an object so I’ll take object name as “s”, now i need to give the marks because
in the initialisation we can see we need to give marks right?, so here i will take marks
as 400, so now if i want to print the marks then i can write like this “s” dot marks
ok like we did in the previous tutorial. To call the percentage method we need to mention
s dot percentage and here we can see this method is returning some value so we need
to print that and here what I’ll do is i will print this percentage symbol after printing
the value ok that’s why here i took percentage now if i save this and run this, here we can
see 400 it will be 66 . 6666 percentage ok so now if i want to change the marks value
then i can take like this object name is s, so s dot marks is equal to 500 now if i check
it will be 83.3333 ok so this will be the percentage, so now everything is good right?,
now imagine that this is the class written by us and many other clients or the persons
are using this class in their program ok this class Student in their program to calculate
the marks of the students. now here we can see we are allowing our clients
to access our attribute directly. so here marks is the attribute of class Student
and we are allowing the clients to change the value of that. so some may ask where is data encapsulation
you are not hiding any data so for that solution is we need to hide the data so we need to
make this marks as the private variable but in Python we know there is nothing called
as private variable, we will just use the name convention, that is we will use two underscore
beginning of the variable to show that that is the private variable right?, so here I’ll
do that same, i will make the marks as the private variable and to access this value
as well as to set this value, here we are accessing the value and we are setting the
value so for this what i will do is i will define two more methods that is setter method
and getter method, to set value as well as to get value ok so in the previous tutorial
to set the attribute we used the setter so here to get the value also we can use getter
method ok so i will define two more methods here the first method will be setter you can
give any name here so setter method is nothing but the method which allow us to set the value
of an attribute ok so here i will take self so here i will set like this. Sorry i want to set right?, so i need a new
value so here I’ll take value and here I’ll assign value so whatever the new value, will
be assigned here in this setter method and for the getter i will return self dot marks
ok so here i will make this as private fine right?, so here we made this marks as the
private variable and we use setter and getter to access the value as well as set the value. ok so now as we defined two more methods,
we need to call that method right?, so to access that we need to call that method. so in the client side what we need to do?,
for this instead of this i will use this ok to set the value i need to call setter method
so i need to mention s dot setter 500. So to print the value to get the value now
i need to use getter method, so print s dot getter ok so now if i save this it will give
the output fine. so here no need to access the marks attribute
now we can use setter and getter method, also setter and getter methods are the ways to
achieve the data encapsulation ok so what we did here is we updated our class, we did
changes to our class and now that will affect on the client side also. Here we can see we can’t write like s dot
marks now, to set the value we need to use s dot setter now. To get the value i need to use s dot getter
now. as i said in the previous tutorial if our
client is writing thousands of lines and if he use this way of setting and getting value
because he used the previous class, where we used to get data like this now he need
to replace this with s dot setter and this with s dot getter, so client won’t be happy
because his code may contain thousands of lines and he can’t sit and do that changes
whole day right?. so there is a solution for this also that
is called as property decorator it will allow us to change the class without affecting the
client side ok so for that we can use the decorator now. so what i will do is i will remove this ok
we don’t want this, we will write like this only. We will use property now ok so here i will
apply this property decorator on this getter because there is no other method which is
returning the marks, here this percentage is entirely different it is returning the
percentage. Here we are changing the marks we are setting
the marks, the method which is returning the marks we need to apply property decorator
on that. so here getter is the one method which is
returning the marks, ok that’s why i need to apply. I’ll put this here, i will tell you why because
as i said we will apply property decorator on getter now ok, so here i will write property. now i need to change this getter name as marks
because here we can see we are printing the marks ok here we are printing the marks, we
are setting the marks OK. so whatever the name here you will take the
attribute name that should be the method name ok and here this will be setter so here this
should also contain same name that is marks and here property decorator should be at the
marks dot setter ok so now if i save this and run this it will give the output 500 and
83. 3333 ok. Here we are changing if i give 600 it will
give 100% this example is little bit different from the previous one right?, so i will show
you the previous example ok this was the previous example, in that what we did is we applied
the property decorator on message, because here we can see we are doing changes in the
message attribute right?, that’s why we took the method name as message ok.
so here setter name is also message here we need to change the getter method names to
marks because here we are applying it on the marks ok so what i will do is i will include
few statements also, this is the getter right?, so here i will write print getting value end
is equal to space, i want control should be in the same line that’s why and here it
will print, setting value to value, ok so now i will show you, setting value 600, getting
value 600 and this value. everything is done ok so now think this class
is used by many clients and one of them came to me and ask me to set the marks limit because
if i enter the marks more than 600 then we don’t want percentage for that right?, students
can get marks up to 600, he can’t get more than that because the final marks, the highest
marks will be 600 right?. if he ask me to set the limit for the marks then we can do
it easily now without affecting the client code, here i can check the value ok so if
value is less than zero value is greater than 600 then it will print can’t set value stick
to previous value. If i save this and if i give the marks as
601 here you need to write else Ok you need to print this. now if i take 601 here can’t set the value
stick to the previous value, so value will be 400 only we won’t set the value ok so
it will give the previous percentage only ok so it is the Setter and getter.
we can delete the value also using deletor method ok so you can define deletor you can
delete the value there, here you can write the statement for that ok you can do that
also ok so this is about the property decorator. Now we will see how to use it as a method
ok so for that you will get all the information. so why i said property is a method built_in
method because here we can see it is belong to class property ok that’s why and this
will be the syntax ok so these are the optionals, property fget that is nothing but getter method
if any getter method is there i need to mention here, setter methods here, deletor method
here and docs here ok. fget is a function to be used for getting an attribute value
like wise fset is a function for setting fdel is a function for deleting ok so we can use
like this and here we can see decorators make diffining new properties or modifying existing
ones easy. so we saw about this right?, how to use property
decorator, now we will see how to use property method, i know you may got confused because
of this naming things right?, so where to give names where to use this property decorator
where to use this setter ok so if you have that problem then don’t worry you can use
this property method easily ok so i will show you how to use that. so i will remove this and i will change the
name also now ok you can give any name here when you are using property method ok built_in
method. So here so i will use setter ok so no need
to do any changes here but in the last line what you need to mention is, ok here in the
last line inside the class should mention marks equal to because i am doing changes
on the marks here ok i did changes on the marks so here i need to write marks equal
to next i need to write property name that is method name so next we need to mention
first getter, next setter, if any deletor, doc documentation ok so first getter so getter
name is here getter, getter method name is getter, next setter ok there is no deletor
method we didn’t define that so i will stop here only. Ok no need to change anything here just at
the last line of the class i need to mention marks, marks because we are doing the changes
on the marks, equal to property getter method name and setter method name ok so now if i
save this and run this we will get the same output here we can see this output was by
using property decorator, this one is now ok same output. So now if i use something like 300 we will
get setting value 300, getting value 300, 50%. So this is about the properties in python,
how we can use it as decorators as well as how we can use it as methods. By using property we can see that we modified
our class and implemented the value constraint without any change required to the client
code. So that’s it for now guys, thank you for
watching if you like this video don’t forget to give a thumbs up, don’t forget to subscribe
to my channel, i will meet you in next class till then take care.