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
• 15,510 points
1,014 views

2 answers to this question.

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
• 58,020 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

Static methods in Python?

Yep, using the static method decorator class MyClass(object): ...READ MORE

answered Dec 2, 2020 in Python by Gitika
• 65,770 points
538 views
+1 vote
3 answers

Difference between append vs. extend list methods in Python

Python append() method adds an element to ...READ MORE

answered Aug 21, 2019 in Python by germyrinn
• 240 points
96,902 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
• 15,510 points
2,271 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,560 points
3,875 views
0 votes
1 answer

Why self is used in methods or init() method in python?

its a constructor like in C++. In c++ ...READ MORE

answered Oct 6, 2020 in Python by Gitika
• 65,770 points
550 views
0 votes
1 answer

Are static class variables possible in Python?

Variables declared inside the class definition, but ...READ MORE

answered Nov 30, 2020 in Python by Gitika
• 65,770 points
578 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,464 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,123 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP