Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a class method in Python?
A class method is a method that is bound to the class and not the instance. It receives the class itself as the first argument, usually named <code>cls</code>, allowing it to access or modify class state.
Click to reveal answer
beginner
How do you define a class method in Python?
You define a class method by decorating a method with <code>@classmethod</code> and including <code>cls</code> as the first parameter in the method definition.
Click to reveal answer
intermediate
Why use <code>cls</code> instead of <code>self</code> in a class method?
<code>cls</code> refers to the class itself, allowing the method to access or modify class variables and create new instances. <code>self</code> refers to an instance of the class, used in instance methods.
Click to reveal answer
intermediate
Can a class method create an instance of the class? How?
Yes, a class method can create an instance by calling <code>cls()</code> with appropriate arguments. This is useful for alternative constructors.
Click to reveal answer
beginner
Example: What does this code output?<br><pre>class Dog:
species = 'Canis'
@classmethod
def get_species(cls):
return cls.species
print(Dog.get_species())</pre>
The output is <code>Canis</code>. The class method <code>get_species</code> accesses the class variable <code>species</code> using <code>cls.species</code>.
Click to reveal answer
What is the first argument of a class method in Python?
Aself
Bcls
Cclass
Dthis
✗ Incorrect
Class methods receive the class itself as the first argument, which is conventionally named cls.
Which decorator is used to define a class method?
A@classmethod
B@staticmethod
C@instance
D@property
✗ Incorrect
The @classmethod decorator marks a method as a class method.
What can a class method access even without an instance?
AInstance variables
BGlobal variables
CClass variables
DLocal variables
✗ Incorrect
Class methods can access class variables via cls, even without an instance.
Which of these is a typical use of a class method?
ATo create alternative constructors
BTo modify instance attributes
CTo handle user input
DTo define private methods
✗ Incorrect
Class methods are often used as alternative constructors to create instances in different ways.
Step 1: Identify variable usage inside class method
The method tries to increment count without cls., causing an error.
Step 2: Correct usage of class attribute inside class method
It should be cls.count += 1 to modify the class attribute.
Final Answer:
Using count without cls prefix inside method -> Option A
Quick Check:
Use cls.count to access class variable [OK]
Hint: Always prefix class vars with cls inside class methods [OK]
Common Mistakes:
Forgetting cls. before class variable
Using self in class method
Missing decorator
5. How can you use a class method to create an alternative constructor that creates an object from a string? Example: Person.from_string('John-25') creates Person('John', 25). Which code snippet correctly implements this?
hard
A. class Person:
def __init__(self, name, age):
self.name = name
self.age = age
@staticmethod
def from_string(data):
name, age = data.split('-')
return cls(name, int(age))
B. class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def from_string(self, data):
name, age = self.split('-')
return Person(name, int(age))
C. class Person:
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def from_string(cls, data):
name, age = data.split('-')
return cls(name, int(age))
D. class Person:
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def from_string(self, data):
name, age = data.split('-')
return cls(name, int(age))
Solution
Step 1: Recognize the use of class method as alternative constructor
The method should be decorated with @classmethod and take cls as first parameter.
Step 2: Parse string and return new instance
Split the string, convert age to int, and return cls(name, int(age)) to create a new object.
Final Answer:
@classmethod with cls parameter returning cls instance -> Option C
Quick Check:
Alternative constructor = @classmethod + cls + return cls(...) [OK]
Hint: Use @classmethod and cls to build alternative constructors [OK]