About 178,000 results
Open links in new tab
  1. Static methods in Python? - Stack Overflow

    Apr 9, 2009 · A builtin example of a static method is str.maketrans() in Python 3, which was a function in the string module in Python 2. Another option that can be used as you describe is the classmethod , the difference is that the classmethod gets the class as an implicit first argument, and if subclassed, then it gets the subclass as the implicit first ...

  2. advantages of the Python static method - Stack Overflow

    disadvantages of the Python static method. It cannot access attributes or methods of the instance or class. The call signature of a @staticmethod is the same as that of a @classmethod or instance method. This masks the fact that the @staticmethod does not actually read or modify any object information. This makes code harder to read.

  3. What is "static method" in python - Stack Overflow

    Sep 9, 2020 · A static method exists independently from your instances (that's why it is named "static"). So a static method is associated with your class definition itself and therefore is always there and therefore can be invoked only at your class itself. It is completely independent from all instances. That's a static method. Python's implementation is a ...

  4. What is the difference between @staticmethod and …

    Sep 26, 2008 · Also observe that this is a good example for using a classmethod and a static method, The static method clearly belongs to the class, since it uses the class Cluster internally. The classmethod only needs information about the class, and no instance of the object.

  5. python - Class (static) variables and methods - Stack Overflow

    Sep 16, 2008 · A class attribute/method is static in Java and C++, no difference, except that in Python the first parameter to a class method call is the class. – Angel O'Sphere Commented Nov 1, 2019 at 8:35

  6. Using static methods in python - best practice - Stack Overflow

    When and how are static methods suppose to be used in python? We have already established using a class method as factory method to create an instance of an object should be avoided when possible. In other words, it is not best practice to use class methods as an alternate constructor (See Factory method for python object - best practice).

  7. python - Static methods - How to call a method from another …

    What follows is mostly to answer "how do you call a static method from another static method": Bear in mind that there is a difference between static methods and class methods in Python. A static method takes no implicit first argument, while a class method takes the class as the implicit first argument (usually cls by convention). With that in ...

  8. python - Check if a method is static - Stack Overflow

    If there's a static method in a base class that's shadowed by a non-static method in a subclass, your function will output True. – Aran-Fey Commented May 20, 2018 at 12:37

  9. Calling static method in python - Stack Overflow

    Jun 10, 2016 · In python 3.x, you can declare a static method as following: class Person: def call_person(): print "hello person" but the method with first parameter as self will be treated as a class method: def call_person(self): print "hello person" In python 2.x, you must use a @staticmethod before the static method:

  10. static methods - What's the point of @staticmethod in Python?

    From the caller's point of view, static methods look like any other method call. If you didn't have @staticmethod available, you could just use a normal instance or class method (albeit at the risk of excess "variable not used!!" linter warnings). So unlike class methods, static methods are an entirely optional part of Python.

Refresh