Method_Decorators_in_Python_Simplified
Method_Decorators_in_Python_Simplified
Method decorators in Python are special decorators used inside classes to modify the behavior of
methods.
1. @staticmethod
- Defines a method that doesn't access instance (self) or class (cls) data.
Example:
class MyClass:
@staticmethod
def greet():
MyClass.greet()
2. @classmethod
Example:
class MyClass:
name = "Python"
@classmethod
def show_name(cls):
MyClass.show_name()
3. @property
Example:
class Circle:
self._radius = radius
@property
def area(self):
c = Circle(5)
print(c.area)
Bonus: @<property>.setter
Example:
class Person:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
@name.setter
self._name = value
p = Person("Alice")
p.name = "Bob"
print(p.name)
Summary Table:
|----------------|----------------------------------|------------------------|