Search code examples
pythondryclass-method

Python "private" classmethod and DRY


Occasionally, a class will have a "private" @classmethod which other methods call:

class FooClassThisSometimesHasALongNameEspIfAUnittestSubclasss(...):
    @classmethod
    def foo():
        ... 

    def bar(self):
        ...
        FooClassThisSometimesHasALongNameEspIfAUnittestSubclasss.foo()
        ...

As can be seen, the class name is repeated; it's admittedly probably not serious enough to cause a meltdown of current technology followed by a zombie apocalypse, but it's still a DRY violation, and somewhat annoying.

The answer to a similar question about super stated that this is one of the reasons for the Py3's new super.

In the absence of some magic normal() function (which, as opposed to super(), returns the current class), is there some way to avoid the repetition?


Solution

  • You could use:

    self.__class__.foo()