Barry Warsaw wrote: > On Fri, 2005-12-09 at 15:38 -0600, Ian Bicking wrote: > > Also decide whether your attributes should be private or not. > > The difference between private and non-public is that the former > > will never be useful for a derived class, while the latter might > > be. Yes, you should design your classes with inheritence in > > mind! > > > > Private attributes should have two leading underscores, no > > trailing underscores. > > > > This conflicts with a previous suggestion "Generally, double leading > > underscores should be used only to avoid name conflicts with attributes > > in classes designed to be subclassed." Or perhaps "private attributes" > > needs to be better explained. > > Maybe the right thing to say is that non-public attributes should always > start with at least one, and usually only one, underscore. If it is a > private attribute of a class that is intended to be inherited from, and > there is a likelihood that subclass attributes may conflict with this > attribute's name, use two leading and no trailing underscores.
I'd prefer language that discouraged double-underscores more since they can't prevent all name conflicts, e.g.: ---------- mod1.py ---------- class C(object): __x = 'mod1.C' @classmethod def getx(cls): return cls.__x ----------------------------- ---------- mod2.py ---------- import mod1 class C(mod1.C): __x = 'mod2.C' ----------------------------- py> import mod1, mod2 py> mod1.C.getx() 'mod1.C' py> mod2.C.getx() 'mod2.C' In this example, there should be two __x attributes, one for the superclass and one for the subclass. But since the name mangling doesn't include the module name, the two classes share the same __x attribute. Note that this problem can arise any time a class and its subclass share the same name. If you have to say something about double-underscores, I'd prefer something like: """ If you're concerned about name conflicts between a non-public attribute of a class and the non-public attributes of its subclasses, some of these can be prevented by using two leading and no trailing underscores. This will not work in all cases however, so sublcasses still cannot be completely ignorant of the non-public attributes of the superclass. """ STeVe -- You can wordify anything if you just verb it. --- Bucky Katt, Get Fuzzy _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com