Role of Instance, Class, Static Methods in Python✌

 


Instance Methods

class Student:

  def __init__(self,name):
    self.name = name
  def printinfo(self):
    self.city = ''Tamil Nadu"
    print('My name is {} and I am from {}'.format(self.name,self.city))
  def printcity(self):
    print('my city is 'self.city)


stud1 = Student('Sam')
stud1.printinfo()


Output:

My name is Sam and I am from Tamil Nadu.

Class Methods

Class methods are created using the @classmethod decorator.

A class method accepts the class as an argument to it which by convention is called clsThis stands for class, and like self, gets automatically passed in by Python.

Class methods don't need self as an argument, but they do need a parameter called clsIt take the cls parameter, which points to the class defined in the program instead of the object of it. It is declared with the @classmethod decorator.

The decorator becomes even more useful when you realize its usefulness in sub-classes. Since the class object is given to you within the method, you can still use the same @classmethod for sub-classes as well.
Class methods are bound to the class and not to the object of the class. They can alter the class state that would apply across all instances of class but not the object state.

class Student:
  Studentcount = 10
  @classmethod
  def printcount(cls):
    print('class is having {} number of students'.format(cls.Studentcount))
Student.printcount()

Output:class is having 10 number of students


class Student:

  Studentcount = 0

  def __init__(self):
    Student.Studentcount += 1

  @classmethod
  def printcount(cls):
    print('class is having {} number of students'.format(cls.Studentcount))

stud1 = Student()
stud2 = Student()

Student.printcount()

class is having 2 number of students

Stud1.printcount() #using instance/object and calling class method

class is having 2 number of students


Static Methods

A static method is marked with a @staticmethod decorator to flag it as static. It does not receive an implicit first argument (neither self nor cls).
It can also be put as a method that “does’t know its class”.
Hence a static method is merely attached for convenience to the class object. Hence static methods can neither modify the object state nor class state. They are primarily a way to namespace our methods.

class Student:
  
  @staticmethod
  def printname(x):
    y='Mr.'
    print('student name is {}{}'.format(y,x))
  
  @staticmethod
  def printage(x):
    print('student age is {}{}'.format(y,x))

>>Student.printname('sam')
student name is Mr.sam
stud1 = Student()
stud1.printage(23)
name 'y' is not defined

Pass members from one class to other

In PYTHON we can pass data members even without concept of inheritance.

It can also use child parent concept to access members and methods of one class into another which is known as inheritance.


Example:

class Student:
  def __init__(self,name,age):
    self.name = name
    self.age = age
  def printage(self):
    print('student age is',self.age)

class Demo:
  
  def changeage(x):
    x.age = x.age+2
    x.printage()


>>stud1 = Student('jack',21)
Demo.changeage(stud1)
student age is 25


Comments

Popular posts from this blog

Access Modifiers in Python✌