0
0
Pythonprogramming~20 mins

Class methods and cls usage in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Class Method Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of class method modifying class variable
What is the output of this code when calling Dog.increment_legs() followed by print(Dog.legs)?
Python
class Dog:
    legs = 4

    @classmethod
    def increment_legs(cls):
        cls.legs += 1

Dog.increment_legs()
print(Dog.legs)
A5
B4
CAttributeError
DTypeError
Attempts:
2 left
💡 Hint
Remember that class methods receive the class as the first argument and can modify class variables.
Predict Output
intermediate
2:00remaining
Output of class method creating instance
What will be printed when running this code?
Python
class Person:
    def __init__(self, name):
        self.name = name

    @classmethod
    def from_full_name(cls, full_name):
        first_name = full_name.split()[0]
        return cls(first_name)

p = Person.from_full_name('Alice Johnson')
print(p.name)
AJohnson
BAlice Johnson
CAlice
DTypeError
Attempts:
2 left
💡 Hint
Look at how the class method processes the full name and what it passes to the constructor.
🧠 Conceptual
advanced
1:30remaining
Understanding cls vs self in methods
Which statement best describes the difference between cls in class methods and self in instance methods?
A<code>cls</code> refers to the instance, while <code>self</code> refers to the class.
B<code>cls</code> refers to the class itself, while <code>self</code> refers to the instance of the class.
C<code>cls</code> and <code>self</code> both refer to the instance but in different contexts.
D<code>cls</code> and <code>self</code> are interchangeable and mean the same.
Attempts:
2 left
💡 Hint
Think about what each method type is designed to work with.
Predict Output
advanced
2:00remaining
Output of class method modifying subclass variable
What will be the output of this code?
Python
class Vehicle:
    wheels = 4

    @classmethod
    def set_wheels(cls, count):
        cls.wheels = count

class Bike(Vehicle):
    wheels = 2

Bike.set_wheels(3)
print(Vehicle.wheels, Bike.wheels)
A3 2
B3 3
C4 2
D4 3
Attempts:
2 left
💡 Hint
Remember that class methods affect the class they are called on, not the parent class.
🔧 Debug
expert
2:30remaining
Identify the error in class method usage
This code is intended to create a new instance with a default name using a class method. What error will it raise when run?
Python
class Cat:
    def __init__(self, name):
        self.name = name

    @classmethod
    def default_cat(cls):
        return cls(name='Whiskers')

c = Cat.default_cat()
print(c.name)
ANo error, prints 'Whiskers'
BTypeError: __init__() got an unexpected keyword argument 'name'
CAttributeError: 'Cat' object has no attribute 'name'
DSyntaxError
Attempts:
2 left
💡 Hint
Check the constructor parameters and how the class method calls it.