Code Explanation:
1. Defining a Class Named Math
class Math:
A class called Math is created.
It will contain a utility function for calculating a cube.
2. Declaring a Static Method
@staticmethod
def cube(n):
return n*n*n
What does @staticmethod mean?
It defines a method that does not require self or an object instance.
It behaves like a normal function, but is grouped inside a class for organization.
You can call it using:
Math.cube(…) (recommended)
or through an object if one exists
Function behavior:
The method receives a parameter n
It returns the cube of n using multiplication: n * n * n
For input 3:
3 * 3 * 3 = 27
3. Calling the Static Method
print(Math.cube(3))
What happens?
No object creation is needed
Math.cube(3) runs the static method
It computes 27
print() prints the returned number
Final Output
27


0 Comments:
Post a Comment