1. NADAR SARASWATHI COLLEGE OF ARTS AND SCIENCE, THENI.
DEPARTMENT OF COMPUTER SCIENCE
STATIC METHOD AND ADDING METHOD
2. STATIC METHOD
Static method in python in comparison to instance methods, are not
bound to an object , object state cannot be accessed or changed by
Python does not automatically give the self or class parameters to
In actuality , you define utility method or group function that have
some logical relationship in a class using static method.
It is very similar to defining a regular function to define static
method inside of a class.
Class name_of_class :
Print(‘static method defined’)
CALLING A STATIC METHOD:
• Without having to create a class instance , a static method can be
called directly from the class.
• Only static variables can be accessed by a static method ; instance
variables are not accessible.
4. Creating a static method using @staticmethod decorator:
add the @staticmethod decorator before the method definition to
make a method static.python includes a built-in function decorator
called @staticmethod that can be used to declare a method as static.
THE STATICMETHOD() FUNCTION:
Some programs may define static method the old-fashioned way by
calling staticmethod() as a function rather than a decorator.
5. ADDING METHODS DYNAMICALLY
The dynamic nature of python you can do many things in runtime , like add
method dynamically to an object or class.
6. Adding method to built-in types
Python doesn’t allow to add method to built-in types , actually to any types
defined in C.The unique way around this is to subclass the type.
for index, item in enumerate(self):
7. Creating dynamic classes in python
Classes can be created dynamically using
Type(name, bases, attributes)
Name: The user defined name of the class
Bases: a list of base classes, and its type is tuple.
Attributes: the data members and methods contained in the class.
8. Create classes dynamically in python
A class defines a collection of instance variables and method to
specify an object type.
A class can be used to make as many object instances of the type of
object as needed.an object is an identified entity with certain
attributes and behaviour
Group of object having similar characteristic and behaviour are the
instance of the same class.
9. Python is a dynamic programming languages and due to its flexibility
python has a significant advantage over statically typed languages.
Python code can be dynamically imported and classes can be
dynamically created at run-time.
Classes can be dynamically created using the type() function in
python. The Type () function is used to return the type of the object.