Python naming conventions for attributes and methods to be rewritten

I have some object oriented code in Python where some classes are designed to be extended to provide missing user bits of code (a la Template method template , but also with variables) that will be used only by the superclass and not the client code using them.

Are there any style conventions for such abstract (or dull, because their implementation in the superclass will either pass or raise NonImplemented ) methods and attributes?

I looked at PEP-0008 and it only mentions adding underscores to private members that are not intended to be used by subclasses.

+6
source share
3 answers

First of all, I think that you are mistaken when you say that:

About adding underscores to private members that are not intended to be used by subclasses.

Actually adding a method / attribute using underscore is a python convention, meaning that this method / attribute should not be accessible outside the class (and its subclass), and I think you forgot to read about the double underscore that is used to make the method / attribute inaccessible for redefinition.

 class Foo(object): def __foo(self): print "foo" def main(self): self.__foo() class Bar(Foo): def __foo(self): print "bar" bar = Bar() bar.main() # This will print "foo" and not "bar". 

There is another way to declare a stub method that uses abc.ABCMeta and abc.abstractmethod .

+9
source

I usually use a single underscore, for example. _myvar for protected (as in C ++) methods / attributes that can be used by derived classes and use double underscore, for example. __var , when it should not be used by anyone else, and double-underlined names at the class definition level are mutilated , so they cannot be redefined in a derived class, for example

 class A(object): def result1(self): # public method return self._calc1() def result2(self): # public method return self.__calc2() def _calc1(self): # protected method, can be overridden in derived class return 'a1' def __calc2(self): # private can't be overridden return 'a2' class B(A): def _calc1(self): return 'b1' def __calc2(self): return 'b2' a = A() print a.result1(),a.result2() b = B() print b.result1(),b.result2() 

It seems here that the derived class B overrides both _calc1 and __calc2 , but __calc2 not overridden, since its name is already distorted with the class name and therefore output

 a1 a2 b1 a2 

instead

 a1 a2 b1 b2 

but ultimately select any convention and document it, also in the above case it’s not so that the base class cannot override the private one, here is the way :)

 class B(A): def _calc1(self): return 'b1' def _A__calc2(self): return 'b2' 
+11
source

There is no naming convention for these methods, as they will have the same name if they are overridden. Otherwise, they will not be overridden! I think that if the method that needs to be overridden does something trivial and documents it accordingly, this is good enough:

 class MyClass: def aMethod(): '''Will be overridden in MyDerivedClass''' pass 

The name mangling, which you mention in your question, is useful if you have a non-trivial method that will be overridden, but you still want access to the base version. See the documentation for more information and an example.

+2
source

Source: https://habr.com/ru/post/899531/


All Articles