Class Inheritance - What am I doing wrong?
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Fri Apr 25 01:10:01 EDT 2008
En Thu, 24 Apr 2008 18:18:01 -0300, Brian Munroe
<brian.e.munroe at gmail.com> escribió:
> Ok, so thanks everyone for the helpful hints. That *was* a typo on my
> part (should've been super(B...) not super(A..), but I digress)
>
> I'm building a public API. Along with the API I have a few custom
> types that I'm expecting API users to extend, if they need too. If I
> don't use name mangling, isn't that considered bad practice (read not
> defensive programming) to not protect those 'private' fields?
Please read this article:
<http://dirtsimple.org/2004/12/python-is-not-java.html>
You don't have to define any getXXX/setXXX methods, just use a public
attribute (if it is supposed to be public, of course). In case you have to
do something special with it (like notifying some observers when the value
changes, by example) use a property instead, and use a *single* leading
underscore in the protected attribute name. In any case, the client code
remains the same: some_object.attribute_name = value
In Python, a single leading underscore means "this is an implementation
detail, don't mess with it". This is a convention and we all -adult and
responsible programmers- follow that convention. Double leading
underscores are a means to avoid name conflicts with subclasses - don't
use them unless you have a valid reason. Double leading and trailing
underscores are __special__ names reserved by Python itself.
--
Gabriel Genellina
More information about the Python-list
mailing list