Hello guys and welcome to Python programming
tutorials by Amuls Academy. Today in this tutorial we are discussing about
the properties in Python, so previously in this series we discussed about the decorators
in detail but in that we took all the user-defined decorators that is nothing but we defined
the decorators, we defined the decorator function we defined the decorator class then we applied
that decorator function or class on the another function or method right?. But there are some built_in decorators also
so in that we are learning three basic important built in decorators. so the first one is called property decorator
the second one is class method decorator and third one is static method decorator. so we are learning these three built_in decorators
so today in this tutorial we will talk about the property decorators, in the next tutorial
we will talk about the class method decorator and static method decorator ok so here in
this property decorator i am dividing it to part 1 and part 2 because, otherwise it will
become very lengthy i need to explain how we can use property decorator as well as i
need to explain how we can use this property as function also that’s why i will divide
this as part 1 and part 2 so today we will do Part 1 in the next tutorial we will do
part 2 and next to next tutorial we will discuss about these decorators ok.
ok so this property decorator is a built_in decorator so we know that why we use decorator
because using decorator we can change the class method or attribute without affecting
the client side code. In the same way this property decorator also
has some purpose. So the first one is, it will allow us to use
class method as attribute ok so in this tutorial i will show you the example for this and we’ll
see how this property will do this and the second purpose is, we can replace setter method
and getter method by using this property decorator. This we will discuss in the next tutorial
along with the property function. ok so now we will see how we can use class
method as attributes in Python. So first of all in python file, i will take
a class because we will use this property decorator on the class so i will take a class
named Student, so next i will take initialisation method, so self I’ll take two attributes first
one is name and second one is grade ok. So here self dot name is equal to name, self
dot grade is equal to grade and here i will take another attribute as “self dot message”,
it will print a message so this will print a message ok self dot name,
got grade self grade ok. So it will print like this if i give name
as Amulya, so Amulya got grade, if i give grade as “B” Amulya got grade B, it will
print like that ok so next this is my class ok it contains only one method that is the
initialisation method. Now i can create an object so I’ll take like
this you can take any object name right?, now i need to pass the name and grade because
here we can see in the initialisation method we can see name and grade so i will pass name
as “nia” and grade as “B” ok so now if i want to print name grade or message then
i can write like this, print ok student 1.name, so grade, message ok.
so now if i save this and run this we can get “nia”,”B”,”nia got grade B”
so if i want to print any message here like name, here we can see name:”nia”, grade:“B”,
nia got grade B. This is fine, so now i want to change the
grade so i can change like this, ok i can mention student1 dot grade and i can mention
some other grade like “A” ok, So now if i save this and run this, it will print name
as “nia”, grade as “A” ok it is changed, but here we can see the message it contains
nia got grade B ok that’s because if you change the value of an attribute of a class
other attributes which are derived from that class won’t automatically update ok so that’s
why here we can see the grade got updated but the attribute message which contains the
self dot grade is not updated ok it won’t automatically update it, that’s why we can
see grade “B” here. so now if any client or any other person is
using your class then they’ll get the wrong output right?, here we can see we are getting
wrong grade here right ?,so we need to fix it, so for that what we can do is, instead
of attribute we can take this in a method OK then we will get the proper output that
is like this, instead of this here we can see the attribute name as message so here
what i will do is, i will define a method called message, so here i will take self ok
so next this will return this. So instead of message attribute i took message
method now ok so now if i execute it as a method ok here we will get the proper output,
“nia”,”A”, nia got grade A. so here we are getting everything perfect
right?, but here we can see we did a change here in the class ok in the class Student,
we converted the attribute to method because of that change our client or the person who
is using this class need to change this message, previously it was an attribute so we didn’t
use this parenthesis, we mentioned like this but after converting it to method to call
that method we need to use the parenthesis. now imagine that many clients or person are
using this class in their project or program and their project or program contains thousands
of lines then wherever they used this message attribute now they need to convert it to method
ok that’s not the easy task and clients won’t be happy with that, it is the extra
work right?. the people who used our previous class where
the message was attribute now they need to convert that to method, if they didn’t change
that then we will get the output like this OK we will get the object. ok we won’t get the proper output so now we
need to make the change to the class otherwise we won’t get the proper output but we don’t
want to make our client unhappy we don’t want to burden them with the changes right?, that
time what we need to do in this case we can use the property decorator which will allow
us to change the class without affecting the client code. so that’s nothing but here if i use the property
decorator here like above this message, we will use property decorator like this at and
property if you remember while explaining about the decorator i said using decorator
is very easy we need to mention the @ symbol followed by the decorator function name or
class name but writing decorator is tough because here we are using the built-in decorator
we just need to mention the name ok so i will just mention property and here we can see
i am using it as attribute ok i am not calling this method now. so now if i save this and run this here we
can see it will give the output ok we are getting the output. This property decorator allow us to use a
method as attribute ok this is the one purpose of the property decorator. so now one of the Other client came to me
and he want to assign here message he want to change the message, for example i will
remove this he want to set the message, like he’ll give message like i will take Amulya
my name got grade A, he will set this message now he want to get name and grade as Amulya
and A OK so now if i save this what will happen?, we will get the attribute error here we can
see can’t set attribute we can’t set attribute like this. now our client came to us, he want to do this
changes and he is asking us to do changes in our class, so we want to value our clients
demand so we need to change in our class ok. so here we want to set the attribute so for
that we need to use setter method ok so the Setter method is nothing but which will allow
us to set the attribute values, so here what i will do is, i will remove this property
decorator ok because first i will show you if i didn’t use any decorator then how this
setter method will affect the client ok so here i am not using any decorator ok so i
can give any name to setter here, so i will give method name as setter, self and message
because i want to set the message so here I’ll take message ok so now what i will do
is i will take a variable, you can take any variable i will take sent, so next i will
split the message now so why because here we can see client will give this message now
to get the name and grade from this i need to split this message right?, so that’s why
i am doing this. message dot split space wherever you will
get space split the message so it will contain Amulya got grade A, so this split will return
a list so sent will be a list it contains Amulya in the zeroth index, got in the first
index, grade in the second index, ‘A’ in the 3rd index ok so if you want i will
print and i will show you so i will just print sent ok so you can see the output later. now i need to get the name and grade so here
i will write self dot name ok as sent the list name and zeroth index because name will
be in the beginning of the message and that will be stored in the zeroth index that’s
why I’ll take name as sent of 0. Ok so Now next i want grade so I’ll take sent
-1 because grade will be present in the last and it will be stored in the last index we
know that the last index is always -1 if i take the negative index then the last value
index will be -1 ok. but here to call this method what we need
to use we can’t write like this right?, we need to write like student1 dot setter then
we need to use this message inside that then it will work we can’t write like this so i
will comment this ok so now if i save this and run this ok so sorry because i didn’t
use parentheses and i didn’t use the property decorator also that’s why here i am getting
the object but here we can see the list, as i said the sent list Amulya got grade A. So
now name will be assigned to amulya grade is “A” here we can see ok so we can set
the attribute value like this. But our client wanted like this right?, now
he didn’t want to mention like this, he want it like this, student 1 dot message as “Amulya
got grade A”, he want to mention like this ok he didn’t want to call the Setter method,
now what to do? so in this cases we can use the property decorator
which will help us ok so here client no need to change anything he can set the attribute
like this but for this here we will mention the property right?, next here what we need
to do is so this will be the Setter method so if client want to mention like this then
i need to change the name of this ok i need to mention it as message because here we can
see, so the method name where we apply the property decorator is message so i need to
mention this method name also message and here i can mention so @ message dot setter
ok message dot setter decorator ok so now if i run this, here we can see in the client
side we set the attribute like this, we didn’t call the setter method. we just mentioned object name dot message
as Amulya got grade A, now if i save this and run this
Here we can see we are getting the output Amulya got grade A, it got updated and we
can see the message. We didn’t get the attribute error. so this is about the property decorator which
allows us to use a method as attribute. in the next tutorial also we will continue
the discussion on this decorator so that’s it for now guys thank you for watching don’t
forget to subscribe to my channel i will meet you in next class till then take care .