Static Method
Static method is a method bound to class and not the object of class. To define static method we will use @staticmethod decorator. class Car: total_car=0 def __init__(self,brand,model): self.brand=brand self.model=model Car.total_car+=1 def full_function(self): return f"{self.brand} {self.model}" def fuel_type(self): return "Petrol or Desiel" @staticmethod def general_description(): return "Cars mean of transportation" class ElectricCar(Car): def __init__(self,brand,model,battery_size): super().__init__(brand,model) self.battery_size=battery_size def fuel_type(self): return "Electric Charge" Tata=Car("TATA","Sedan") Mahindera=Car("Mahindera","Geep") Tesla=ElectricCar("Tesla","ModelX","85KWH") print(Car.general_description()) print(Tata.general_description()) Output: Cars mean of transportation Cars mean of transportation Explanation: If you want somebody not to access specific function through object you can do it by applying the @staticmethod. And do not pass 'self' because it will create connection with other attribute. But if you seen closely general_description is accessible through object and class also. So the question arises does this statement defy us. Well answer is, Yes, it does. Here is analogy: You will kept your screwdriver, hammer or nickel in toolbox, so that somebody want to access it he/she will look for toolbox. If you travel through airplane doesn't which model of airplane you travel the manual of that plane will remain same like how to tie seatbelt. Same as goes for static method you just access static method function through class not by object and it doesn't defy its meaning. OOPs concept is philosophical you follow the rule so that you can have strategy how write code effectively.

Static method is a method bound to class and not the object of class. To define static method we will use @staticmethod decorator.
class Car: total_car=0 def __init__(self,brand,model): self.brand=brand self.model=model Car.total_car+=1 def full_function(self): return f"{self.brand} {self.model}" def fuel_type(self): return "Petrol or Desiel" @staticmethod def general_description(): return "Cars mean of transportation" class ElectricCar(Car): def __init__(self,brand,model,battery_size): super().__init__(brand,model) self.battery_size=battery_size def fuel_type(self): return "Electric Charge" Tata=Car("TATA","Sedan") Mahindera=Car("Mahindera","Geep") Tesla=ElectricCar("Tesla","ModelX","85KWH") print(Car.general_description()) print(Tata.general_description())
Output: Cars mean of transportation Cars mean of transportation
Explanation:
- If you want somebody not to access specific function through object you can do it by applying the @staticmethod.
- And do not pass 'self' because it will create connection with other attribute.
But if you seen closely general_description is accessible through object and class also. So the question arises does this statement defy us.
Well answer is, Yes, it does.
Here is analogy:
You will kept your screwdriver, hammer or nickel in toolbox, so that somebody want to access it he/she will look for toolbox.
If you travel through airplane doesn't which model of airplane you travel the manual of that plane will remain same like how to tie seatbelt.
Same as goes for static method you just access static method function through class not by object and it doesn't defy its meaning.
OOPs concept is philosophical you follow the rule so that you can have strategy how write code effectively.