Python: Understanding classmethod

0 11
Avatar for RisingStar
1 year ago

The code I am providing is runnable, just run it in any python interpreter or IDLE and you will see how I mentioned step-by-step flow of program using print() statement.

# using built-in classmethod as decorator
# classmethod allows changes to class. This change can even be made by classreference and by object reference( Yeah, thnx to this method, it is possible)

class Student:
    no_of_Leaves = 8
    def __init__(self,naam,grad,umar):
        self.name= naam
        self.grade = grad
        self.age = umar


    def printDetails(self):  # self refers to the object using it, i.e if harry calls this func self = harry
        print(f"Name is {self.name} \nGrade is {self.grade} \nAge is {self.age}")


    @classmethod # 1 . This line has the same function as "change = classmethod(change)"  down there. so if you uncomment that one and comment this one
    # it won't make any difference, but don't uncomment both or you will have to deal with an error
    

    def change(cls,modifiedleaves):
        cls.no_of_Leaves = modifiedleaves

    # change = classmethod(change)



harry = Student("harry",10,21)

# let's see how class method works

print("checking initial value of leaves: ")# checking initial value of leaves
print("Value of leaves in Student:",Student.no_of_Leaves,"-------Harry leaves: ",harry.no_of_Leaves)   # prints 8

print("\nlet's try changing it using class reference") # changing class variable value using class-reference "Student.no_of_leaves"
Student.no_of_Leaves = 10
print("\nafter changing it \nValue of leaves in Student:",Student.no_of_Leaves,"-------Harry leaves: ",harry.no_of_Leaves)

print("\nlet's try changing it using object(harry) reference")
harry.no_of_Leaves = 12
print("\nafter changing it \nValue of leaves in Student:",Student.no_of_Leaves,"-------Harry leaves: ",harry.no_of_Leaves)
print("\n See the difference? You can't change class variable value using object that's why a new instance variable was created for harry having leaves: 12")

print("___________________________________________________________________________________________________________________________________________________")
print("""\nNow, let's see the Power of classmethod, but first, let me delete that instance variable with no_of_leaves 12____ so that' we have a clear"
      "understanding of what is happening""")

print("\nBefore deleting instance variable \nValue of leaves in Student:",Student.no_of_Leaves,"-------Harry leaves: ",harry.no_of_Leaves)
del harry.no_of_Leaves
print("\nAfter deleting instance variable \nValue of leaves in Student:",Student.no_of_Leaves,"-------Harry leaves: ",harry.no_of_Leaves)

print("See? Back to 10 and 10")

print("\nLet's Try changing it using classmethod while calling it using class \"Student\" " )
Student.change(12)
print("\nafter changing it \nValue of leaves in Student:",Student.no_of_Leaves,"-------Harry leaves: ",harry.no_of_Leaves)

print("\nLet's Try changing it using classmethod while calling it using object \"harry\" " )
harry.change((14))
print("\nafter changing it \nValue of leaves in Student:",Student.no_of_Leaves,"-------Harry leaves: ",harry.no_of_Leaves)

print("""\nSee the power of classmethod? Above, while directly trying to change it using object(harry) a new variable was created,
 but now, it managed to modify the existing class variable so that's why class method is useful""")



0
$ 0.00
Sponsors of RisingStar
empty
empty
empty
Avatar for RisingStar
1 year ago

Comments