It is a good idea to call staticmethod in python on yourself and not on the class name itself

If I have the following class.

class Foo:
    @staticmethod
    def _bar():
        # do something
        print "static method"

    def instancemethod(self):
        Foo._bar()  # method 1
        self._bar() # method 2

In this case, method 1 is the preferred way to call staticmethod _bar () or method 2 in the Python world?

+4
source share
1 answer

Write a code that expresses what you want to do. If you want to call this method right here:

class Foo:
    @staticmethod
    def _bar():  # <-- this one
        # do something
        print "static method"

then specify this specific method:

Foo._bar()

If you want to call anything that self._barpermits, then you actually decided that it makes sense to override it and make sure that your code still behaves wisely when this method is overridden, and then specify self._bar:

self._bar()

, , , , , , , , Foo._bar().

+4

All Articles