python development practices?
Cliff Wells
logiplexsoftware at earthlink.net
Thu Nov 1 14:58:26 EST 2001
On Wednesday 31 October 2001 15:57, Chris Tavares wrote:
> This scenario is precisely why the two underscore name mangling scheme was
> added to python. Rewrite your example as:
>
> class Base:
> def __init__(self, x):
> self.__x = x
>
> def print_base_x(self):
> print self.__x
>
> class Derived( Base ):
> def __init__(self, base_x, dev_x):
> Base.__init__(self, base_x)
> self.__x = dev_x
>
> def print dev_x(self):
> print self.__x
>
> d = Derived( 5, "hi there!" )
>
> d.print_base_x()
> prints 5
>
> d.print_dev_x()
> prints Hi There!
>
> So, the mechanism you're asking for is already there. Wether it's pretty or
> not is a different question. But it does work, and it's implemented now.
Admittedly this is true. Unfortunately, it requires double underscores on
_every_ attribute you want protected (that is, when using the "black
box"/every-attribute-changes-by-get/set-methods). Not only ugly, but rather
non-Pythonic (IMHO). All I'm saying is it would be *nice* to have a
statement specifying that all the attributes within a certain scope are
private (in effect, doing the double underscore behind the scenes).
Others have suggested that since you can read the source for a class you are
deriving from, this is a non-issue. However, if I derive from some class
(perhaps from the standard library), I don't want to be burdened with
verifying that there are no collisions between the base class's attributes
and any attributes I may have added in my derived class. Especially since a
new version of the base class may introduce new attributes that cause the
collision. Unless _every_ class distributed for public consumption has all
of it's attributes prefixed with double underscores, this is bound to happen.
As far as people who want to derive from a class and have access to those
protected attributes, they will obviously need to read the source for the
base class anyway, so really, it doesn't place any additional burden on them
(except perhaps, having to make a few attributes public in the base class -
not a big deal).
Regards,
--
Cliff Wells
Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308
(800) 735-0555 x308
More information about the Python-list
mailing list