-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassmethod_vs_staticmethod_github.py
38 lines (32 loc) · 1.47 KB
/
classmethod_vs_staticmethod_github.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Calculator:
# Class variable to track total calculations
total_calculations = 0 # Keeps track of how many calculations have been performed
@staticmethod
def add(a, b):
"""
Performs addition of two numbers.
- Does not depend on any class or instance variables.
- Static methods are used for simple, independent operations.
"""
return a + b
@classmethod
def increment_calculations(cls):
"""
Increments the class variable 'total_calculations'.
- Uses the 'cls' parameter to access and modify class-level data.
- Ideal for operations that need to track or modify the state of the class itself.
"""
cls.total_calculations += 1
return f"Total calculations so far: {cls.total_calculations}"
# Main program
if __name__ == "__main__":
# Use the static method to add numbers
result = Calculator.add(5, 7) # Addition of two numbers
print(f"Addition Result: {result}") # Output: 12
# Call the class method to update and display calculation count
print(Calculator.increment_calculations()) # Increment and print count: 1
# Perform another calculation using static method
another_result = Calculator.add(10, 20)
print(f"Another Addition Result: {another_result}") # Output: 30
# Increment and display calculation count again
print(Calculator.increment_calculations()) # Increment and print count: 2