Python class method
Python class method is a method that is bound to a class rather than its object. It doesn’t require creation of a class instance.
Python is an object oriented programming language. Everything in python is an object. The key part of an object oriented programming is classes and objects.
What is class and object?
class is collection of data member and member function. Also we can say that class is a collection of objects. For ex: Let’s take a real life example of computer science class all of the student belongs to the computer means all of them having similar computer science behavior.
Where object is a run time entity. Object means anything that physical exist like Mobile, pen, projector, laptop etc.
For example in image above, there is an class named student. Class is an logical representation of an object we can say blue-print of an object. Where aman, harry and john these are objects of that class. In class above name and age are the properties(data member) of the class.
If you want to learn more about OOPs then you can visit:
Types of methods:
As we discuss earlier class consists of:
- Data Members (Variables)
- Member Functions (methods)
In python there are three types of methods :
- Instance Method (object method)
- Class Method
- Static Method
Instance or object methods are very common and widely used. Object methods are the functions that belongs to the object. Basically Instance method are methods which require an object of its class to be created before it can be called. To invoke/call a instance method, we have to create an Object of the class in within which it defined.Object method:
- Can modify object’s state
- They Can modify class state
- Also can accept more than one parameters
- Instance method belongs to the object of a class not to the class i.e. they can be called only after creating object of the class.
- Every individual object created from the same class has its own copy of the instance methods of that class.
class classname: def funName(self): pass objname =classname() objname.funName()
In example above, we have accessed parameters (a,b) of one function to another (myfun()) using self keyword.
Through the self parameter, instance methods can freely access attributes and other methods on the same object. This gives them a lot of power when it comes to modifying an object’s state.
Not only can they modify object state, instance methods can also access the class itself through the self.__class__ attribute. This means instance methods can also modify class state.
Self is not a reserved keyword in python. Basically it is a pointer, that points to a specific object/ instance of a class. The instance method uses self as a first parameter. It is not compulsory to name it self only, you can use anything instead self, but it is compulsory to take it (a reference variable ) as a first parameter in instance method.
Python Class Method:
A Python class method is a method that is bound to a class rather than its object. It doesn’t require creation of a class instance. Instead of accepting a self parameter, class methods take a cls parameter that points to the class and not the object instance when it is called. Here is some important points about class method:
- Using class method, we can pass a class as an argument to manipulate class variables.
- The class method takes the class as a first argument.
- Here we will use decorator (@classmethod) at the top of method.
- Class method refers to only class not an instance of class.
- They cannot modify object instance’s state because they do not deal with object creation(it requires self).
- Can modify class state (as they take class as a parameter)
- It can be access by using object or by using class.
class classname: attName = "attValue" def funName(cls): pass classname.funName()
In example above, you can see we have not created object of the class myclass() , we can access , alter the data members and member function directly. Also we have passed a reference variable cls that refers to the class itself ,Unlike self which refers object of that class. So it is the concept of Python class method.
Static methods are the methods in Python which we can call without creating an object of class. They are referenced by the class name itself or reference to the Object of that class. This type of method takes neither a self nor a cls parameter. it’s free to accept an arbitrary number of other parameters. Therefore a static method can neither modify object state nor class state. Static methods are restricted in what data they can access. Important points about static method:
- Static method(s) are associates with the class in which they reside i.e. they can be call even without creating an instance of that class i.e ClassName.methodName(args).
- When you have code that can be share across all instances of the same class, put that portion of code into static method.
- They are basically used to access static fields of the class.
- They are designed with aim to be shared among all Objects created from the same class.
- Can’t modify object’s state because they does not use self.
- Also can’t modify class state because they does not use cls.
- It is mark with a @staticmethod decorator to flag it as a static method.
class classname: @staticmethod def funName(): pass objname =classname() objname.funName() or classname.funName()
In example above, we have created an staticmethod fun(), which can access the class attribute course by myclass.course but it cannot alter the object and class’s state.
Instance method vs Class method vs Static method
- Instance method can access the instance methods and instance variables directly.
- Also instance method can access static variables and static methods directly.
- Class method can’t access the instance methods and instance variables directly.
- Class method can access class’s global variable ,static variables and static methods directly.
- Static methods can access the static variables and static methods directly.
- Static methods can’t access instance methods and instance variables directly. They must use reference to object. And static method can’t use self keyword as there is no instance for ‘self’ to refer to.
So it is all about the methods in class. Thanks.