Predict the Method Output
Intermediate · Predict the output · from Python @classmethod vs @staticmethod: When to Use Each
From the lesson: Python @classmethod vs @staticmethod: When to Use Each
Imagine a company with three kinds of employees. Regular employees work on their own tasks (they need to know about themselves). Managers can hire new employees for the whole department (they need to know about the department).
Read the lessonRead the code and predict the output. Pay attention to which method type each is.
class Counter:
count = 0
def __init__(self):
Counter.count += 1
@classmethod
def get_count(cls):
return cls.count
@staticmethod
def info():
return 'I count things'
a = Counter()
b = Counter()
c = Counter()
print(Counter.get_count())
print(Counter.info())class Counter:
count = 0
def __init__(self):
Counter.count += 1
@classmethod
def get_count(cls):
return cls.count
@staticmethod
def info():
return 'I count things'
a = Counter()
b = Counter()
c = Counter()
print(Counter.get_count())
print(Counter.info())
More Classmethod and Staticmethod exercises
- Create a Factory MethodBeginner
- Add a Static ValidatorBeginner
- Fix the Factory MethodIntermediate
- Build a Temperature ConverterAdvanced
All Python exercises · 50 practice problems · Object-Oriented Programming quiz