Hello welcome to this tutorial on class methods and static methods in order to understand these two
methods.
We will look at an example so here we have a library class and this glass contained a class attribute
books so the class variable books is basically a list of items or list of Titan's now coming to the
init method in the index method.
We have defined two parameters the student I.D. title of the book and then we initialize these two arguments.
The student I.D. and the title of the book.
Next we have two methods in this class.
One is the detailed method and the other is update list method.
Many details method is called using an instant.
It returns the student I.D. That is to whom the book is issued and the title of the book issued.
Next update list method once a book is issued.
It needs to be removed from the list of available books in the library and the method update list does
that in order to issue books.
I need to create an instance of the class library and calling the instance method using the instance.
Will you give me details of the transaction.
That is it gives me details such as the student I.D. and the title of the book and also update the collection
of the books in the library in order to access these instance variables the student I.D. the title and
the list of books available in the library.
I need to first have an instance so an object needs to be created first and then we can call instance
methods using this object.
Now let us say we want to add more books to the collection in the library and to do this.
I do not need to create an instance of the class first so I need to update the list of books.
In the class attribute and for that I do not need to create an instance.
So say for example you will create an instance.
Eric and me will pass the parameters say the student I.D. is 1 2 3 4 and then the title of the book
that is available in the collection in the library is 8.
Now I have another requirement.
I want to add more books to the collection and the library and in order to do this I don't need to create
an instance of the class first.
So that means I want to update the list or the class variable books and for that in order to do this
I do not need any instance of an object in order to update this list.
So I want to keep adding books to the collection without having to create an instance.
And this is possible using class methods.
Here is the class method.
Add to list and to this method we have parsed two parameters.
One is to see a list parameter which means see a list means the class and then we have another list
as a parameter.
A list of books and in this method we are concatenate thing the new list to the old list and then returning
the new list of books for the instance.
Methods like details method and the update list method we have past self as the parameter and self refers
to the instance of a class when the method is gone.
When we pass see a list as a parameter to the method the parameter C is points to the class and not
the object instance when the method is called.
So we do not need to create an instance before calling class methods so class methods are not bound
to an object or an instance but they're bound to a class so we pass the first parameter as seamless
and non self and class method.
Also useful for defining alternate constructors meaning the main constructor of a class is defined in
the init method.
So another example we have these two parameters defined in the input method coming to the class method
actor list it takes two parameter d see a list parameter and the other is new collection which contains
a list of books that are to be added to the collection in the library.
Using the class method actor list we have defined a new constructor a new set of parameters have been
defined using this class method.
And Python only allows one init method but class so using class method.
It is possible to add as many alternate constructors as necessary in order to be able to use a method
as a class method.
We need to mark the method with a decorator like this
and a decorators have to immediately proceed a method or a function and they start with the add sine
unlike the instance methods coming to the instance method.
We do not need to have any decorator any method that we create in a class will automatically be created
as an instance method unless we tell Python otherwise.
And here is the class library.
So let's go ahead and execute this.
And in this class library we have.
As discussed we have a class attribute books and a constructor the init method.
And apart from this we have to instance method.
Both of these instance methods.
Take one parameter self which bind to an instance of the class library.
The new method is called so we have discussed that class method are bound to the class and not an instance
that has instant methods are bound to the instance so let us try calling the the instance method without
creating an instance in the first place.
So let us see what will happen.
Letters call the instance method transaction using the class library and it does not take any parameters
apart from the self parameter.
Let's execute this line.
So here we have the unbound method transaction must be called with an instance of the class as the first
argument so without creating an instance of the class library we can not execute or call the method.
The Instance methods that are defined in the class so we have not yet defined an instance.
Now coming to class method add to list class methods as we have discussed before and methods that are
not bound to an object but an instance.
But they are bound to a class
so class method deal with class itself this means we can use the class and it attributes inside this
method rather than dealing with a particular instance
and in order to use the method actor list as a class method we have decorated this class with this add
class method decorator Noda to flag it as a class method and took this class method the first parameter
that we have passed is C S and this bind to the class and A.D. object instance as we have discussed
before.
So let us try calling this class method using the class itself
so library da add to this and to this method we will pass a list
of books C we will pass in by J K just like we do not pass any values to the self parameter in case
of instance method we do not have to pass a new value to the C a list parameters.
Python does it for us automatically so let's call this method I do list using the class library and
then execute so hard as the output.
So these are the items that I have been on the books that have been added to the collection.
And this is of a new collection of books that are available in the library.
So without even creating an instance we have changed the state of the class and now in order to call
the instance methods letters create an instant C the instant is Eric student I.D..
And the title of the book is C..
B let's pass these two arguments to the class library and execute.
Eric is an object or an instance of the library class.
Now we can now go ahead and call the instance methods using this instance.
Eric so that a dot transaction so here we have the output from the instance method transaction.
Let's call the second method.
Update list and after removing the book that has been issued to the student.
This is the updated list.
So we have talked about class method which are bound to a class itself and not to any instance of a
class coming to static methods a static method is a method that knows nothing about the class or the
instance it was called on.
So Biden doesn't have to instantiate a bound method for an object.
And also we do not have to parse the parameters clause or save as implicit first arguments do a static
method.
So let us see an example.
In the class library we have added another method known as a static method.
So where you have decorator the method issued by using the at static method decorator.
And if you observe we have not parsed the self parameter which points to an instance or the class parameter
which bind to a class object to this static method.
So a static method knows nothing about a class or an instant but it just deals with these parameters
that we have past so static method can neither access the instance did not the class state the work
like regular functions but they belong to the classes namespace and when do we need to define static
method in a class.
So for example when we need some functionality which is not with reference to an object but with reference
to the complete class we can define a static method.
Say for example when we need a functionality that doesn't access any property of a class but makes sense
that it belongs to the class we use static methods.
So here in this example I have defined the static method issued by.
And this takes an amateur name.
Let us change this to library an undisclosed name and add this function displays the name of the librarian
who issues the book within this particular function.
We are not accessing any of the properties of a class not changing the state of a class or an instant.
So let execute this and you can call the static method using the class so here we have called the issued
by static method using the class library.
And when we execute this the print function displays this message.
So this type of method takes needed a self or not a class parameter but it can accept arbitrary number
of other parameters.