MCS-201 · Unit 13

Classes in Python

Section 5 of 7

Block 4 · Unit 13Classes in Python8 min · Section 5 of 7

13.5Instance, Class and Static Methods

Covers IGNOU MCS-201 §13.5

Not every method needs a particular object. Some work with the class as a whole, and some need neither the object nor the class. Python distinguishes three kinds of methods for these cases.

Three kinds of methods

Instance methodClass methodStatic method
Decoratornone@classmethod@staticmethod
First parameterself (the object)cls (the class)none required
Can accessInstance and class variablesClass variables (through cls)Only its own parameters
Called asobj.method()Class.method() or obj.method()Class.method() or obj.method()
Typical useWork on one object’s dataAlternative constructors; work with class-wide dataUtility functions related to the class

A decorator is the @name line placed just above a def; it modifies the function defined below it (decorators are covered in Unit 15).

Static (class) variables

The study material calls class variables “static variables”. They have one copy shared by all objects. Read and change them through the class name, as described in the previous section.

Key takeaways

  • Instance method: self, works with one object.
  • Class method: @classmethod, cls, works with the class; useful as an alternative constructor.
  • Static method: @staticmethod, no special first parameter, a utility.
  • Class (“static”) variables are shared by all objects.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which decorator defines a class method?

Show answer

Answer: B. @classmethod

@classmethod makes Python pass the class as the first argument.

Question 2 · multiple choice

What does the first parameter of a class method receive?

Show answer

Answer: C. The class

By convention it is named cls and refers to the class the method was called on.

Question 3 · multiple choice

What does this program print?

Python · quiz_static.py
class Calc:
    @staticmethod
    def add(a, b):
        return a + b

print(Calc.add(2, 3), Calc().add(4, 5))
Show answer

Answer: A. 5 9

A static method can be called through the class or an object; it receives only the arguments passed.

Question 4 · multiple choice

Which kind of method should create an object from a different format, such as a CSV string?

Show answer

Answer: D. Class method

A class method receives cls and can return cls(...), which also works correctly for subclasses.

Question 5 · multiple choice

What does this program print?

Python · quiz_count.py
class Item:
    made = 0
    def __init__(self):
        Item.made += 1
    @classmethod
    def how_many(cls):
        return cls.made

Item(); Item(); Item()
print(Item.how_many())
Show answer

Answer: C. 3

Each constructor call increases the shared class variable; the class method reads it.

Question 6 · short answer

Explain with an example when you would use a static method rather than an instance method.

Show worked answer

Use a static method when the operation is related to the class but needs no data from any particular object or from the class. For example, in a Temperature class, @staticmethod def c_to_f(c): return c * 9 / 5 + 32 converts any Celsius value; it is called as Temperature.c_to_f(100) and returns 212.0. An instance method would require creating an object first for no reason.

↑ ↓ move↵ openTab switch type