-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path04_static_methods.py
More file actions
27 lines (22 loc) · 815 Bytes
/
Copy path04_static_methods.py
File metadata and controls
27 lines (22 loc) · 815 Bytes
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
class MathUtils:
"""
Demonstrating @staticmethod for utility functions.
Static methods do not take 'self' or 'cls' as arguments.
They behave like regular functions but are namespaced within a class.
"""
@staticmethod
def is_even(number):
return number % 2 == 0
@staticmethod
def calculate_tax(amount, rate=0.15):
return amount * rate
# Testing the implementation
if __name__ == "__main__":
# Accessing static methods via the class name
print(f"Is 10 even? {MathUtils.is_even(10)}")
amount = 1000
tax = MathUtils.calculate_tax(amount)
print(f"Tax on ${amount} at 15% is: ${tax}")
# Static methods can also be accessed via an instance (though less common)
utils = MathUtils()
print(f"Is 7 even? {utils.is_even(7)}")