Advertisement
# Python - Creating a class method
class Test:
# Defining a class method using @classmethod decorator
@classmethod
def class_meth(cls):
print('Hello from the class method')
# Python - Creating a class method
class Test:
# Defining a class method using @classmethod decorator
@classmethod
def class_meth(cls):
print('Hello from the class method')
# Calling the class method by using the class name with the dot operator
Test.class_meth()
Hello from the class method
Advertisement
# Python - Creating a class method
class Test:
# Defining a class method using @classmethod decorator
@classmethod
def class_meth(cls):
print('Hello from the class method')
ob = Test()
# Calling the class method by using the object name with the dot operator
ob.class_meth()
Hello from the class method
# Python - A static method
class Test:
# Defining a class method using @classmethod decorator
@classmethod
def stat(cls):
print('Hello from the class method')
# Defining a general method
def method(self):
#Calling the class method from within this general method by using self keyword i.e. current object.
#In this case, an object name is internally replaced by its class name while calling a static method
self.stat()
#Calling the class method from within this general method using the class name
Test.stat()
# Creating an object of class Test
ob = Test()
# Calling the method()
ob.method()
Hello from the class method
Hello from the class method
# Python - A general method overshadowing a class method
class Test:
# Defining a class method using @classmethod decorator
@classmethod
def stat():
print('Hello from a class method')
# Defining a general method with the same name as a class method
# Hence, the definition of class method will be overshadowed i.e. replaced by this general method
def stat(self):
print('Hello from a general method')
# Creating an object of class Test
ob = Test()
# This will call the general method and not the class method
# because the general method definition has overshadowed i.e. replaced the class method definition
ob.stat()
# Calling the class method using its class name will only lead to an error because it doesn't exist anymore
Test.stat()
Hello from a general method
Traceback (most recent call last):
File "D:/Python Programs/class_method2.py", line 31, in <module>
Test.stat()
TypeError: stat() missing 1 required positional argument: 'self'
# Python - Creating a class method
class Test:
# Defining a class variable
message = 'Have a good day'
num = 19
# Defining a class method using @classmethod decorator
@classmethod
def class_meth(cls):
print('message : ', cls.message)
print('num : ', cls.num)
cls.message= cls.message + ' and keep smiling :-)'
cls.num = 5.5
print('Updated message :', cls.message)
print('Updated num : ',cls.num)
# Creating an object of Test class
ob = Test()
# Calling the class method by using the object name with the dot operator
ob.class_meth()
message : Have a good day
num : 19
Updated message : Have a good day and keep smiling :-)
Updated num : 5.5
# Python - A static method
class Test:
# Defining a static method using @classmethod decorator
@classmethod
def stat():
print('Hello from the static method')
# Incorrect direct access of a class method
stat()
Traceback (most recent call last):
File "D:/Python Programs/class_method3.py", line 15, in <module>
stat()
NameError: name 'stat' is not defined
# Python - Behaviour of class method changes depending on the object of class which has called it.
class Fruit:
@staticmethod
def name():
print('It is a fruit')
@classmethod
def message(cls):
if(cls.__name__=='Apple'):
print('Apple')
elif(cls.__name__=='Mango'):
print('Mango')
# Creating a class Mango inherited from class Fruit
class Mango(Fruit):
pass
# Creating a class Apple inherited from class Fruit
class Apple(Fruit):
pass
# Creating an object of Apple
ob_apple = Apple()
# Calling the class method, message, using the object of Apple class, because it is inherited by Apple class
ob_apple.message()
# Creating an object of Mango
ob_mango = Mango()
# Calling the class method, message, using the object of Mango class, because it is inherited by Mango class
ob_mango.message()
# Calling the static method, message, using the object of Apple class, because it is inherited by Apple class
ob_apple.name()
# Calling the static method, message, using the object of Mango class, because it is inherited by Mango class
ob_mango.name()
Apple
Mango
It is a fruit
It is a fruit
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement