There is a special type of method that Python language offers, this is called class method.
This function is neither static nor instance.
Notation is compulsory while we are going to declare this kind of function.
This function can also be used to create static variables.
Class method contains the class reference implicitly which means one argument is necessary while we are going to create class method.
Script1: Write an script which demonstrate the usage of class method in Python language.
class Demo:
@classmethod
def fun(cls):
cls.a=10
Demo.b=20
Demo.fun()
print(Demo.a)
print(Demo.b)
Output:
10
20
Script2: Write an script to print all the static variables exist in the class.
class Demo:
x=80
@classmethod
def fun(cls):
cls.a=10
Demo.b=20
Demo.fun()
print(Demo.__dict__)
This function is neither static nor instance.
Notation is compulsory while we are going to declare this kind of function.
This function can also be used to create static variables.
Class method contains the class reference implicitly which means one argument is necessary while we are going to create class method.
Script1: Write an script which demonstrate the usage of class method in Python language.
class Demo:
@classmethod
def fun(cls):
cls.a=10
Demo.b=20
Demo.fun()
print(Demo.a)
print(Demo.b)
Output:
10
20
Script2: Write an script to print all the static variables exist in the class.
class Demo:
x=80
@classmethod
def fun(cls):
cls.a=10
Demo.b=20
Demo.fun()
print(Demo.__dict__)
Be First to Post Comment !
Post a Comment