1. NADAR SARASWATHI COLLEGE OF ARTS AND SCIENCE, THENI.
DEPARTMENT OF COMPUTER SCIENCE
STATIC METHOD AND ADDING METHOD
DYNAMICALLY
Presented by:
B.POORANI
I-MSC(CS)
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
static method.
Python does not automatically give the self or class parameters to
static method.
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.
3. EXAMPLE
Class name_of_class :
@staticmethod
def name_of_static_method(parameters_list):
pass
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.
SYNTAX :
name_of_class.static_method_name()
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.
SYNTAX:
Staticmethod(function)
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.
Class person(object):
Pass
def play():
print(“I’m playing”)
P=person()
p.play=play
p.play()
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.
class UpperList(list):
pass
def to_upper(self):
for index, item in enumerate(self):
self[index]=item.upper()
UpperList.to_upper=to_upper
L=UpperList([‘I’,’g’,’o’,’r’])
l.to_upper()
Print (l)
7. Creating dynamic classes in python
Classes can be created dynamically using
SYNTAX:
Type(name, bases, attributes)
Parameters:
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.
SYNTAX:
Type(object)