0% found this document useful (0 votes)
12 views7 pages

Python Decorter

Uploaded by

yogitas804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Python Decorter

Uploaded by

yogitas804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Class Method Decorator

@classmethod
In Python, the @classmethod decorator is used to declare a method
in the class as a class method that can be called
using ClassName.MethodName(). The class method can also be
called using an object of the class.

The @classmethod is an alternative of the classmethod() function. It


is recommended to use the @classmethod decorator instead of the
function because it is just a syntactic sugar.

@classmethod Characteristics
 Declares a class method.
 The first parameter must be cls, which can be used to access
class attributes.
 The class method can only access the class attributes but not
the instance attributes.
 The class method can be called
using ClassName.MethodName() and also using object.
 It can return an object of the class.

The following example declares a class method.

class Student:
name = 'unknown' # class attribute
def __init__(self):
self.age = 20 # instance attribute

@classmethod
def tostring(cls):
print('Student Class Attributes: name=',cls.name)

Student.tostring() #Student Class Attributes: name=unknown

Above, the Student class contains a class attribute name and an


instance attribute age. The tostring() method is decorated with
the @classmethod decorator that makes it a class method, which can
be called using the Student.tostring(). You can call the class method
as classname.method() or using class object object.method().
Note: The first parameter of any class method must be cls that can
be used to access the class's attributes. You can give any name to
the first parameter instead of cls.

The class method can only access class attributes, but not the
instance attributes. It will raise an error if trying to access the
instance attribute in the class method.

class Student:
name = 'unknown' # class attribute
def __init__(self):
self.age = 20 # instance attribute

@classmethod
def tostring(cls):
print('Student Class Attributes: name=',cls.name,',
age=', cls.age)

Student.tostring() #calling class method

The class method can also be used as a factory method to get an


object of the class, as shown below.

class Student:
def __init__(self, name, age):
self.name = name # instance attribute
self.age = age # instance attribute

@classmethod
def getobject(cls):
return cls('Steve', 25)

std = Student.getobject()
print(std.name) #'Steve'
print(std.age) #25

@classmethod vs @staticmethod
The following table lists the difference between the class method
and the static method:
@classmethod @staticmethod

Declares a class method. Declares a static method.

It can access class attributes, but not the instance It cannot access either class attributes or instance
attributes. attributes.

It can be called using It can be called using


the ClassName.MethodName() or obje the ClassName.MethodName() or obje
ct.MethodName(). ct.MethodName().

It can be used to declare a factory method that It cannot return an object of the class.
returns objects of the class.

Define Static Method using


@staticmethod Decorator in
Python
The @staticmethod is a built-in decorator that defines a static
method in the class in Python. A static method doesn't receive any
reference argument whether it is called by an instance of a class or
by the class itself.

@staticmethod Characteristics
 Declares a static method in the class.
 It cannot have cls or self parameter.
 The static method cannot access the class attributes or the
instance attributes.
 The static method can be called
using ClassName.MethodName() and also
using object.MethodName().
 It can return an object of the class.

The following example demonstrates how to define a static method


in the class:

class Student:
name = 'unknown' # class attribute
def __init__(self):
self.age = 20 # instance attribute

@staticmethod
def tostring():
print('Student Class')

Above, the Student class declares the tostring() method as a static


method using the @staticmethod decorator. Note that it cannot
have self or cls parameter.

The static method can be called using


the ClassName.MethodName() or object.MethodName(), as shown below.

#calling static method


Student.tostring() #'Student Class'
Student().tostring() #'Student Class'

std = Student()
std.tostring() #'Student Class'

The static method cannot access the class attributes or instance


attributes. It will raise an error if try to do so.

class Student:
name = 'unknown' # class attribute

def __init__(self):
self.age = 20 # instance attribute

@staticmethod
def tostring():
print('name=',name,'age=',self.age)

Student.tostring() #error
Python Property Decorator -
@property
The @property decorator is a built-in decorator in Python for
the property() function. Use @property decorator on any method in
the class to use the method as a property.

You can use the following three decorators to define a property:

 @property: Declares the method as a property.


 @<property-name>.setter: Specifies the setter method for a
property that sets the value to a property.
 @<property-name>.deleter: Specifies the delete method as a
property that deletes a property.

Declare a Property
The following declares the method as a property. This method
must return the value of the property.

class Student:

def __init__(self, name):


self.__name = name

@property
def name(self):
return self.__name

Above, @property decorator applied to the name() method.


The name() method returns the private instance attribute
value __name. So, we can now use the name() method as a property
to get the value of the __name attribute, as shown below.
s = Student('Steve')
print(s.name) #'Steve'

Property Setter
Above, we defined the name() method as a property. We can only
access the value of the name property but cannot modify it. To
modify the property value, we must define the setter method for
the name property using @property-name.setter decorator, as shown
below.

class Student:
def __init__(self, name):
self.__name=name

@property
def name(self):
return self.__name

@name.setter #property-name.setter decorator


def name(self, value):
self.__name = value

Above, we have two overloads of the name() method. One is for


the getter and another is the setter method. The setter method
must have the value argument that can be used to assign to the
underlying private attribute. Now, we can retrieve and modify the
property value, as shown below.
s = Student('Steve')
print(s.name) #'Steve'

s.name = 'Bill'
print(s.name) #'Bill'

Property Deleter
Use the @property-name.deleter decorator to define the method that
deletes a property, as shown below.

class Student:
def __init__(self, name):
self.__name = name

@property
def name(self):
return self.__name

@name.setter
def name(self, value):
self.__name=value

@name.deleter #property-name.deleter decorator


def name(self):
print('Deleting..')
del self.__name

std = Student('Steve')
del std.name
print(std.name) #AttributeError

The deleter would be invoked when you delete the property using
keyword del. Once you delete a property, you cannot access it
again using the same instance.

You might also like