PCPP 32 10x PCPP Certified Professional Python Programmer 1 · Free Practice Question Medium
Question 26
Consider the following Python class:
- class MyClass:
- base = 10
- @classmethod
- def modify_base(cls, new_base):
- cls.base = new_base
If you instantiate two objects of this class and modify the base attribute using the modify_base class method in one of the instances, what will be the output when you print the base attribute of both instances?
-
A
Only the
baseattribute of the instance where the class method was called will reflect the new value. -
B
The
baseattribute of both instances will reflect the new value. -
C
The
baseattribute of both instances will remain unchanged. -
D
The
baseattribute of the instance where the class method was not called will reflect the new value.
Reveal correct answer
Correct answer: B
A.
@classmethod modifies the class attribute, not just the attribute of a specific instance.
B.
This is correct because the @classmethod decorator allows the method to modify class state that applies across all instances of the class.
C.
The modify_base method is designed to modify the base attribute of the class.
D.
This is incorrect because the @classmethod modifies the class state and not just specific instance state.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
