Class Inheritance - What am I doing wrong?
Arnaud Delobelle
arnodel at googlemail.com
Fri Apr 25 01:11:45 EDT 2008
Brian Munroe <brian.e.munroe at gmail.com> writes:
> 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?
The problem is that you are using name mangling for an attribute which
is accessed by several generations of a class hierarchy. Name
mangling is only useful for attributes you *don't* want to share with
subclasses (or bases).
In python, use attributes starting with a single underscore (such as
_name). It tells users that they shouldn't mess with them. By
design, python doesn't include mechanisms equivalent to the Java / C++
'private'.
--
Arnaud
More information about the Python-list
mailing list