Lab9
Lab9
Objective:
• Implement static and class methods within a class in Python.
• Compare the usage of static and class methods with instance methods.
Introduction:
class MyClass:
def method(self):
return 'instance method called', self
@classmethod
def classmethod(cls):
return 'class method called', cls
@staticmethod
def staticmethod():
return 'static method called'
Class Method: Class methods are the type of method used when a method is not really about
an instance of a class, but the class itself. To create a class method, just add '@classmethod'
a line before creating the class method. The class is automatically the first argument to be
passed in, and is represented as 'cls' instead of 'class'. This is because 'class' is already
assigned to be something else in Python.
Syntax Python Class Method:
class C(object):
@classmethod
def fun(cls, arg1, arg2, ...):
• A class method is a method that is bound to the class and not the object of the class.
• They have the access to the state of the class as it takes a class parameter that points to
the class and not the object instance.
• It can modify a class state that would apply across all the instances of the class. For
example, it can modify a class variable that will be applicable to all the instances
Static Method: A static method does not receive an implicit first argument. A static method is
also a method that is bound to the class and not the object of the class. This method can’t
access or modify the class state. It is present in a class because it makes sense for the method
to be present in class.
class C(object):
@staticmethod
def fun(arg1, arg2, ...):
Static methods are different from regular methods and class methods in that it doesn't have a
class or instance that is automatically passed in as a first positional argument. They can be
created by adding '@staticmethod' a line before defining the method. These are methods that
have a logical connection to the Class, but does not need a class or instance as an argument. It
is better to make sure we create a static method rather than class or regular method when we
are sure that we don't make use of the class or instance within the method.
@staticmethod
def get_max_value(x, y):
return max(x, y)
Explanation:
MyClass has a constructor (__init__) that initializes an instance variable value.
It also has a static method (get_max_value) that takes two parameters (x and y) and returns
the maximum of the two values using the max function.
An instance of MyClass is created with obj = MyClass(10).
The static method get_max_value is called using the class name
MyClass.get_max_value(20, 30). It returns the maximum of 20 and 30, which is 30.
The static method is also called using an instance obj.get_max_value(20, 30). This is a valid
way to call a static method, but it is generally recommended to call static methods using the
class name.
Example:
from datetime import date
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class MathOperations:
pi = 3.14159 # Class variable
class StringUtils:
@staticmethod
def is_palindrome(word):
cleaned_word = ''.join(char.lower() for char in word if char.isalnum())
return cleaned_word == cleaned_word[::-1]
@classmethod
def count_vowels(cls, text):
vowels = 'aeiou'
return sum(1 for char in text.lower() if char in vowels)
Explanation:
char.isalnum()
• This checks if each character (char) in the input string (word) is an alphanumeric
character (a letter or a digit).
char.lower()
• This converts each character to its lowercase form. This is done to make the
comparison case-insensitive.
''.join(...)
• This joins the characters that satisfy the conditions (alphanumeric) into a new string.
cleaned_word
• is a string that contains only alphanumeric characters in lowercase.
cleaned_word[::-1]
• This creates a reversed version of cleaned_word using slicing.
Task:
1. Write a python program using static and class methods.