Advertisement
# Python - Defining a static method
class Test:
# Defining a static method using @staticmethod decorator
@staticmethod
def stat():
print('Hello from the static method')
# Python - A static method
class Test:
# Defining a static method using @staticmethod decorator
@staticmethod
def stat():
print('Hello from the static method')
# Calling the static method using its class name
Test.stat()
Hello from the static method
Advertisement
# Python - A static method
class Test:
# Defining a static method using @staticmethod decorator
@staticmethod
def stat():
print('Hello from the static method')
#Creating an object of class Test
ob = Test()
# Calling the static method using the object of its class
ob.stat()
Hello from a static method
# Python - A static method
class Test:
# Defining a static method using @staticmethod decorator
@staticmethod
def stat():
print('Hello from the static method')
# Defining a general method
def method(self):
#Calling the static method from within this general method 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 static 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 a static method
Hello from a static method
# Python - A general method overshadowing a static method
class Test:
# Defining a static method using @staticmethod decorator
@staticmethod
def stat():
print('Hello from a static method')
# Defining a general method with the same name as a static method
# Hence, the definition of static 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 static method
# because the general method definition has overshadowed i.e. replaced the static method definition
ob.stat()
# Calling the static 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/static15.py", line 27, in <module>
Test.stat()
TypeError: stat() missing 1 required positional argument: 'self'
# Python - Modifying class/static variable from within a static method
class Test:
# A class/static variable
i=1
@staticmethod
def stat():
Test.i = Test.i+10 # A class variable is modified by accessing it with its class name
# Accessing the class/static variable using its class name
print('i = ',Test.i)
#Calling the static method using its class name
Test.stat()
# Accessing the class/static variable using its class name
print('i = ', Test.i)
i = 1
i = 11
# Python - A static method
class Test:
# Defining a static method using @staticmethod decorator
@staticmethod
def stat():
print('Hello from the static method')
# Incorrect direct access of a static method
stat()
Traceback (most recent call last):
File "D:/Python Programs/static13.py", line 15, in <module>
stat()
NameError: name 'stat' is not defined
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement