Friends here our concept is
class and object attributes.
See already we know simply what is the class. So class is
a template or blueprint to create an object.
So once if you have a template from that we can create any
number of objects. And class is the combination of attributes
and methods. So basically attributes are like variables,
[no audio]
and we can define attributes for class and objects.
I mean for class level we can define some variables, and for
object level we can define some variables.
Let me explain this concept with the help of a simple Python
script. So I am saving my script name as 'working_on_attributes.py'.
So guys suppose I am writing a simple Python script
or program to maintain the data for employees.
Suppose assume that for time being we are having some
two employees.
Right. Then,
I want to maintain the data for these two employees, then
before going to maintain the data first
I will assume, I am going to create one template. So I want
to store so and so things for employee.
[no audio]
Right. So then what I am doing is, 'class', simply 'emp'.
That's it. ':', then enter.
So I am defining
one method to store
the employee name, age and salary suppose.
So let me define
[no audio]
as 'get_name_age_salary'.
[no audio]
That's it. So actually, this is a function.
But whenever if you want to write a function inside of a
class, you have to write by default 'self' as the key, and you
don't need to worry about the value for 'self'. Automatically
'self' will be replaced by the particular object. From which
object you're calling that object will come in the place of 'self'.
That's fine.
Now you are expecting for this function 'name', 'age', and 'salary'.
Let me take variables as, I mean I will pass arguments for
this method while calling like 'name',
'age' and 'salary'. That's it.
Then I want to store this 'name', 'age', and 'salary' in a memory
location. So for that you have to write 'self.name=name'.
So guys don't get confused, 'self.name' is different, and
'name' is different.
It is like 'xy = y', completely they are different variables.
Right. Then 'self.age = age'.
So if you are getting confusion just take here arguments
as different variables,
I mean different names like 'n', 'a', 's', but if you take 'name',
'age', 'salary', just somewhat meaningful
that's why I'm taking arguments as 'name', 'age' and 'salary'. Then
'self.salary'.
That's it. Then I am going to define one more function just to display
the employee details.
Then what I can do, 'display_emp_details', or simply
'display_details
[no audio]
(self)'. So actually to display employee details
what we need? 'name', 'age', and 'salary'.
So if you are using normal function like 'display_details',
if it is a normal function, to get your details you need to
pass arguments to this function, but this is a method and
if you observe these two methods are in the same class, that's
why whatever the variables you are having you define in your
first method, those variables you can access in other method.
That's why I'm not passing any arguments to 'display_details'
method. Directly
I can access from my previous method.
So that's why directly
what I am doing is, simply 'print(
"The name is:". So simply here
I will write 'name'. Name is nothing but 'self.name', but
not this 'name' guys.
This you're passing to this method while calling.
Okay. But actual variables or attributes for your objects
are these things.
Then let me write "/n The age is: ", you can write anything.
No problem. Just for your understanding
I'm writing simple script. Then, "The salary is: ".
[no audio]
That's it. Let me write salary, 'self.salary'. That's it.
Right. Then write simply 'return None'.
[no audio]
So I'm not expecting any return value from this method.
So here also I'm not expecting any return value,
that's why I'm writing simply 'return None'. That's fine.
So guys, if you run your code as of now, you're not getting
any output and you are not going to get any error because
this is just a template. Templates won't execute whenever you
run. It will execute whenever if you assign this template
to any object.
Right. That's fine.
Now see that. Just assume that we are having two employees,
say 'emp1' and 'emp2'.
[no audio]
Okay.
Now I want to store the details for 'emp1' and 'emp2',
and I want to see whenever if I need. So to store we have
this method, and to display we have this method, second method.
Right. So instead of 'get' let me 'assign', or yeah, no problem.
You can take any method, right?
So 'emp1'.
So before going to store 'emp1' details, what you have to do?
First of all, you need to assign this template for this object.
Right. Then only these methods and these attributes or variables,
you can access from 'emp1'.
That's why, what I'm doing? First
I am doing 'emp()', then for 'emp1()', and 'emp2()'.
That's it.
Right. So guys as of now you have 'emp1' and 'emp2'
objects, but there is no information for 'emp1'
and 'emp2'. Because, if you execute any one of the method,
then only you're going to store your data. Suppose to store your data
you have to execute this method, then you need to call that
method. You know how to call a method of your class.
Now, assume that this entire class, this template is stored
in the name of 'emp1' and 'emp2'. So whenever if
you call from 'emp1', 'emp1.get_name_age_salary'.
Let's say I am giving as suppose
'name', anything like John,
[no audio]
'age' suppose 34,
then I am giving 'salary' as something like,
[no audio]
just assume it may be 45000 per month. Just assume that.
Right. Fine.
Now, the same way I want to store my data, my 'emp2' data, then
just write
[no audio]
That's it. Okay.
Now if I execute, while running your program somewhere in
memory location two memory blocks will be created. One memory
block for 'emp1' and second memory is for 'emp2'.
Right. And see before calling your 'display' if I write 'dir(emp1)'
See the result, what you are getting.
[no audio]
Basically for 'emp1', what are the information
you are having? 'name',
I mean this method and variables, right, and one more method
here. Now see the output.
Anyway, these are the default values.
Other than that, see that. 'age' is there, 'name' is there, 'salary'
is there. So 'emp1.age', 'emp1.name', 'emp1.salary'.
The same way you can use, this is the method,
these two are the methods. We know
that. By observing here
we can understand that these two are methods.
So 'emp1.display_details()'.
'emp1.get_name_age_salary'.
But while calling if you need arguments, you need to pass.
The same way you can also run this for
'emp2'.
[no audio]
Same information is there for 'emp2' also
but the values are different.
That's it.
Now fine. Just observe this guys. While calling from your 14th line
or 'emp1.get_name_age_salary
("John", 34, 45000)', values
will be assigned to respectively 'name', 'age' and 'salary'. Then
what about 'self'?
'self' by default from which object you are calling, that object
'name' will be stored in 'self'.
That's why in memory it is going to create like
'emp1.name' not 'self.name'.
Okay. For our purpose, for dynamic purpose we are writing 'self',
but while storing your values, your variables in memory location
they are going to store in the respective by using 'emp1',
'emp1.name', 'emp1.age', 'emp1.salary'.
That's it. And one more thing, whenever you are accessing
your variables inside of a class, always you have to take
'self.name', 'self.age', 'self.salary', like that.
But for same 'emp1'
if you want to access your variables, attributes outside of
a class, outside of a class, then you have to access with
'emp1.' not 'self.'. Be clear. Outside you have to
access with the 'emp1.'. Suppose
I want to display 'age'. See the result what you are getting.
34. That's fine.
And if you observe even method we are also calling as
'emp1.'. Suppose if I want to call
'display' method, how can I call? 'emp1.display_details'. That's it.
If you observe 'display_details' is not requiring any arguments,
it is not taking any arguments except 'self'.
Anyway, 'self' is going to store with the help of 'emp1'.
'emp1.name' will store into 'self', that's why we are passing
correct arguments while calling 'display' method.
Now just observe the result.
[no audio]
And one more thing, you are calling your method outside of a class.
Suppose I want to display my details after assigning the
data to your 'self.name', 'age' and 'salary',
then you have to call this method inside of this first method.
So whenever if you want to call your method inside of a method
then you have to write 'self.', method name.
[no audio]
So 'self' means outside you can assume this is like 'emp1',
but inside of a class if you want to call and if you
want to execute any method you have to write 'self.'. Now,
let me comment this and see the result. You're going to get,
'display_details' for two employees. See that.
First you are assigning. Because of third fourth and fifth
lines, you are assigning your values for your attributes
or variables. Then immediately you're calling the respective
object 'display_details' method.
That's why you're going to get the same values in the output. That's it.
You are assigning, and then you are displaying. That's
it. So be clear.
If you want to execute a method inside of a class, you have
to write 'self.', but outside of a class
you have to write your object name, '.', that method name.
[no audio]
That's fine. And if you observe this entire program, you are having
these are like attributes.
[no audio]
Right. And these attributes, these attributes are specific for
particular object because we are writing 'self.'. 'self' is
nothing but object name.
So these attributes are related to your object.
That's fine. Now,
I have a small requirement. After creation of, after creation
of your objects.
I need to find at last how many employee objects have been
created using this template.
[no audio]
How many employee objects have been created by using this
template. I have to know that. Count
I need to find out. See what I am doing is,
I am writing 'count'. 'count =', initially 0.
[no audio]
So whenever if I create 'emp1', at that time I need
to increase this value, I need to increase this value.
This value is not related with your object.
This is related with your template, your class.
This is class variable. See that. If I want to access that
variable from outside of your class
I need to access with the 'emp.count'.
What is 'emp' here? Class name.
0, you're getting. Let me comment.
[no audio]
By default we are calling 'display_details', right. Now
just observe the output.
[no audio]
You're getting 0.
Right. I can increase this value at any time, may be inside of a
class, inside of a method, or outside of a class. See,
I am doing 'emp.count = emp.count + 1'.
So after that, I am printing my 'emp.count'.
[no audio]
That's it.
Now this variable, if you define any variable outside of your
methods without 'self', then that is for your template or
for your class attribute.
That is a class attribute.
Now, what I am doing is, I am taking the help of this variable
and whenever if I create an object at that time I will increase this value.
I can increase either inside of a class or inside of a method
or outside of a class. See how I am going to do that.
See whenever if I create these values.
Right. That means you're creating an object, right?
So in this I'm going to create one more method.
You can create any method.
I mean like 'increase_count', 'increase_
[no audio]
count_for_employee', not for your object.
[no audio]
But method is we are writing inside of your class, right. See
what I am doing is, simply I am writing 'emp'.
this is the class name, then 'count = emp.count + 1'.
Of course, you can also write your shortcut for your
arithmetic operation. Then simply 'return None'. That's it.
[no audio]
See, but I need to manually run this,
I mean I execute this method whenever if I create an object,
so I mean after giving your 'name', 'age', and 'salary' for any employee,
after that I need to call 'emp1.increase_count_for_emp()',
[no audio]
then 'emp2.increase_count_for_emp()'.
That's it. Now see the result.
Yeah, let me print so that you can understand.
[no audio]
So what is the variable? 'emp.count'. See the result.
Two variables have been, sorry two objects have been created
by using this template.
Try to understand this variable, normal variable, right, like
'x =', something. 'count = 0'. Now, if you define
the variable immediately after writing your class, class name,
if you define any variable, right, that variable is related
for your class.
So this is called class attribute.
So why we need class attribute means, how many, suppose
one of the example I am giving
but you can define any number of variables based
on your requirement.
The very simple thing I am giving is, from the template how many
objects has been created?
To count that I am defining one variable, one attribute called
'count'. That's it.
So if you don't want to call externally your increase count
for employee just comment these two methods here.
So whenever if you create your variables immediately call
'self.increase_count_for_emp'.
That's it. Now see the result. Same output, right. 2, you're getting.
Okay. So guys, this is all about simply class and object attributes.
Be clear. These are class object attributes, and this is for your class
attribute. And remember that class attributes we have to call
or we have to save like 'self.', your variable name. Even
while displaying that we have to take 'self.'. That is only
inside of a class.
But if you come outside, then you have to take instead of
'self', particular object name, 'emp1.name', 'emp2.name',
something like that. But for class variable, this
is class variable or class attribute, 'count'.
So either inside of a method, inside of a class, or outside
of a class, you can always access with the help of 'emp',
I mean template, class name, '.', your attribute.
[no audio]
Be clear. For 'emp' object inside 'self', outside 'emp1',
or 'emp2', outside, inside 'self.'. But whereas
for class variables, attributes wherever you go, I mean
in any line in your program, you have to take always 'emp.count',
I mean your class name, '.', that variable. That's it.
Okay. Okay guys, thank you for watching this simple video.
[no audio]