0% found this document useful (0 votes)
7 views5 pages

Python Class Methods Explained

The document explains the Demo class in Python, illustrating instance methods, class methods, and static methods. It details how the instance method initializes object attributes, the class method creates new instances from a list of arguments, and the static method performs operations without class or instance context. Examples demonstrate the creation of objects and the use of these methods effectively.

Uploaded by

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

Python Class Methods Explained

The document explains the Demo class in Python, illustrating instance methods, class methods, and static methods. It details how the instance method initializes object attributes, the class method creates new instances from a list of arguments, and the static method performs operations without class or instance context. Examples demonstrate the creation of objects and the use of these methods effectively.

Uploaded by

Sathish Smart
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OOPS

instance, class, static methods

A R U N A R U N I S T O
class Demo:
#instance
def __init__(self, name, age):
[Link] = name
[Link] = age

#class method
@classmethod
def add_args(cls, args):
return cls(*args)

#static method
@staticmethod
def add_name(val):
return val

def __str__(self):
return f"Name: {[Link]} | Age: {[Link]}"

This Demo class demonstrates the concepts


of instance methods, class methods, and
static methods in Python:
1. Instance Method (__init__):
This method is called when an
instance of the class is created.
It initializes the instance variable’s
name and age with the values passed
during object creation.

2. Class Method (add_args):


This method is decorated with
@classmethod, making it a class
method.
It receives the class itself (cls) as its
first parameter, conventionally
named cls.
It takes a list of arguments (args) and
returns a new instance of the class
with those arguments passed to the
constructor.
By using cls, it can create and return
a new instance of the class, which is a
common use case for class methods.
[Link] Method (add_name):
This method is decorated with
@staticmethod, making it a static
method.
It doesn't receive the class or
instance as its first parameter (no self
or cls).
It performs a simple operation on the
input value val and returns it.
Static methods are often used for
utility functions that don't depend on
instance or class state.

>>> from workoutfile import Demo


>>> obj = Demo("Arun", 25)
>>> print(obj)
Name: Arun | Age: 25
>>> obj2 = Demo.add_args(("Arunisto", 28))
>>> print(obj2)
Name: Arunisto | Age: 28
>>> obj.full_name = obj.add_name("Arun Arunisto")
>>> obj.full_name
'Arun Arunisto'
1. An instance of the Demo class named obj
is created with the name "Arun" and age
25.
2. The __str__ method is called implicitly
when print(obj) is executed, providing a
formatted string representation of the
object.
3. Another instance of the Demo class
named obj2 is created using the add_args
class method, which takes a tuple of
arguments and returns a new instance of
the class.
4. The full_name attribute is dynamically
added to the obj instance by calling the
add_name static method and assigning
its return value.
5. Finally, the full_name attribute is
printed, displaying "Arun Arunisto".

You might also like