Python: Regular method and static method with same name

While it’s not strictly possible to do, as rightly pointed out, you could always “fake” it by redefining the method on instantiation, like this:

class YourClass(object):

    def __init__(self):
        self.foo = self._instance_foo

    @staticmethod
    def foo():
        print "Static!"

    def _instance_foo(self):
        print "Instance!"

which would produce the desired result:

>>> YourClass.foo()
Static!
>>> your_instance = YourClass()
>>> your_instance.foo()
Instance!

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)