Code Explanation:
๐น 1. Class Definition
class Test:
✅ Explanation:
A class named Test is created.
This class contains a variable and two special methods.
๐น 2. Class Variable
x = 10
✅ Explanation:
x is a class variable.
It is shared by all objects of the class.
Can be accessed using:
Test.x
cls.x (inside class methods)
๐น 3. Class Method
@classmethod
def show(cls):
return cls.x
✅ Explanation:
@classmethod decorator makes this method a class method.
It takes cls (class reference) as the first parameter.
๐ What happens:
cls refers to the class (Test)
cls.x → accesses class variable x
✔️ Returns:
10
๐น 4. Static Method
@staticmethod
def display():
return Test.x
✅ Explanation:
@staticmethod defines a method that:
Does NOT take self or cls
Acts like a normal function inside class
๐ What happens:
Directly accesses class using:
Test.x
✔️ Returns:
10
๐น 5. Calling Methods
print(Test.show(), Test.display())
✅ What happens:
➤ Test.show()
Calls class method
cls = Test
Returns:
10
➤ Test.display()
Calls static method
Returns:
10
๐ฏ Final Output
10 10

0 Comments:
Post a Comment