Instance, Class, and Static Method
The ‘method’ terminology is quite popular whenever we are discussing about programming languages that support Object-Oriented Design.
During the process of creating the template(i.e. Class), we would have specified the attributes(e.g. legs, eye, weight, etc) and the actions(e.g. run, move, etc) that the real cat(instance) must exhibit.
Technically, those actions are referred to as methods. Methods are ‘Functions’ (‘Actions’ to a layman) that reside inside of the class.
Instance method, as the name suggests, is tied to a real object(instance) after it has been created from the Template(Class). This set of methods can act or work only on an instance of a Class.
Time to write code 🧑💻️. Implementation in Pytton
[*]
class Cat:
def __init__(self, name, age):
self.name = name
self.age = age
We defined the Template/Blueprint/Class for creating a real cat object by utilizing the ‘class’ keyword .
Here we listed the attributes we expect as name and age.
__init__ is called a constructor(a special kind of method). It’s main purpose is to populate a real new object(cat) with its attributes at the point of creation.
[*]’self’ (some languages called it ‘this’) refers or points to this real object(instance) that is being created.
With this template up here, creating new real cats becomes easy.
cat1 = Cat(‘Bingo’, 2)
cat2 = Cat(‘Nerd’, 1)
so we can easily know the name and age of different cats
[*]cat1.name1
Bingo
[*]cat1.age
2
Instance Method:
Remember we mentioned ‘self’ (‘this’ in some languages) earlier. The only way we are able to create an instance method in python is by mentioning self as the first parameter passed to a function declaration.
class Cat:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self):
print(f'{self.name} is eating')
We define a method `eat` (it is function but called method since it resides in a class) and passed the first param as self. So this make it
an instance method because we would be able to call it on every instance(real object) we create.
[*] Usage
cat1 = Cat('Bingo', 2)
cat2 = Cat('Nerd', 1)
cat1.eat()
cat2.eat()
[*]Output
Bingo is eating
Nerd is eating
So we were able to instruct the two cats(Bingo and Nerd) to eat.
[*]Quick Note:
- Instance method are able to acess their internal state(attribe) using self.name_of_attribute.
They have access to their class using self.__class__ and are able to modify their class state(attribute)
- __init__ as mentioned earlier is a constructor method(used in the constructor of an instance) and a specifically used by python.s So it is
impossible to call it on those instances(cat1 and cat2).
- Using ‘self’ as used above is the convention in the python community and it’s highly encouraged you stick to it. Using ‘this’ or any other would work
Class method can be accessed through the class just as the instance method can be accessed through an instance. It can be utilized whenever we want make changes across all the instances.
Say two cats come from the same parent. an attribute like the parent name(surname) should be widely shared across the class so that whenever it is modified it reflects across all other instances
class Cat:
surname = 'Something'
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def change_name(cls, name):
cls.surname = name
[*]Usage
cat1 = Cat('Bingo', 20)
cat1.surname
[*]Output
‘Something’
cat2 = Cat('Nerd', 4)
cat2.change_name('New Name')
[*]cat1.surname
‘New Name’
So in the above example cat1 was able to know when the surname changed despite that the surname was changed by another cat.
In the above example, we called the class method on an instance(cat2).
Since it is a Class method, calling it on the class would work well.
[*]Cat.change_name(‘Another name’)
Ready to elevate 🚀 your Python 🐍 skills? Check out YouTube for exclusive content. Consider subscribing to stay updated.
Class method takes the first parameter as the cls(this refers to the class) and also decorated with @classmethod.
It works directly on the Class without any instantiation
Static Method are normal functions that sits in a class. It is widely used as utils(utility function).
Static method does need access to instance or class.
class Cat:
surname = 'Something'
def __init__(self, name, age):
self.name = nameh
self.age = age
@staticmethod
def get_date():
return now
It is decorated with @staticmethod and does not need an instance to be created before usage.
It’s method works directory on the Class
Cat.get_date()
Thanks for reading and feel free to explore my video collection on YouTube for more educational content. Don’t forget to subscribe to the channel to stay updated with future releases.