Static methods in Python?

+1 vote

Is it possible to have static methods in Python so I can call them without initializing a class, like:

ClassName.StaticMethod ( )

Aug 1, 2018 in Python by bug_seeker
• 14,970 points
44 views

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Yes this can be acheived by using staticmethod decorator

class MyClass(object):

   @staticmethod def the_static_method(x):

      print x MyClass.the_static_method(2

      # outputs 2

Note:-  Code might use the old method of defining a static method, using staticmethod as a function rather than a decorator. 

class MyClass(object):

   def the_static_method(x):

      print x the_static_method = staticmethod(the_static_method) 

      MyClass.the_static_method(2

      # outputs 2

This is entirely identical to the first example (using @staticmethod), just not using the nice decorator syntax.

answered Aug 1, 2018 by Priyaj
• 56,120 points
+1 vote
Static methods are used when we need to process data associated with classes instead of instances. A static method has no self argument and it is nested in a class and is designed to work on class attributes instead of instance attributes.Static methods never receive an automatic self argument, whether called through a class or an instance. They usually keep track of information that spans all instances, rather than providing behavior for instances.
answered Aug 4, 2018 by Ronaldobessmin
• 160 points

edited Aug 4, 2018 by Vardhan

Related Questions In Python

0 votes
1 answer

Difference between append vs. extend list methods in Python

append: Appends object at end. x = [1, ...READ MORE

answered Jun 4, 2018 in Python by ariaholic
• 7,320 points
106 views
0 votes
1 answer

Difference between append vs. extend list methods in Python

append: Appends object at the end. x = ...READ MORE

answered Aug 8, 2018 in Python by bug_seeker
• 14,970 points
53 views
0 votes
1 answer

Pycharm warning: Must implement all abstract methods in Python. Why?

n vote As expected, python itself recognises that ...READ MORE

answered Nov 9, 2018 in Python by Nymeria
• 3,500 points
220 views
+3 votes
7 answers

How can I rename a file in Python?

yes, you can use "os.rename" for that. ...READ MORE

answered Mar 31, 2018 in Python by DareDev
• 6,520 points
54 views
+2 votes
2 answers

How can I create a new file in Python?

You can try the below code which ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,420 points
42 views
+2 votes
3 answers

what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,420 points
951 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
19 views
+4 votes
6 answers

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.