GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — Python staticmethod decorator: @staticmethod
Python staticmethod decorator: @staticmethodThe static method decorator, or @staticmethod, is used to define methods inside a class as static methods. For example: class CodesCracker: @staticmethod def statfun(x): print("Value of x:", x) CodesCracker.statfun("Hey!") Here is its output: Value of x: Hey! Note: Static methods do not have access to what the class is. The static method is just a function, without having access to the object of the class (where the static method is defined) or its internals. The differentiation between these two is provided in class methods versus static methods. Note: Also, there is no "self" or "cls" parameter for the static method. When we need to define a normal static method inside a class that can be called directly using the class, we need the @staticmethod decorator to do the task. Python @staticmethod decorator syntaxThe syntax of the @staticmethod decorator in Python is given below. @staticmethod def myfun(arg1, arg2, arg3, ..., argN): # definition code goes here Python @staticmethod decorator exampleHere is a simple example of the @staticmethod decorator in Python. This program employs the @staticmethod syntax to define static methods within the "CodesCracker" class: class CodesCracker: @staticmethod def myfun(a, b, c, s): print("The value of a:", a) print("The value of b:", b) print("The value of c:", c) print("The value of s:", s) CodesCracker.myfun(100, 200, 300, "Python is Fun!") The sample output produced by this Python program is shown in the snapshot given below: Advantages of the @staticmethod decorator in Python
Disadvantages of the @staticmethod decorator in Python
« Previous Tutorial Next Tutorial » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |