"Private" attributes - a possible idea (maybe PEP-worthy).
Delaney, Timothy
tdelaney at avaya.com
Thu Jan 31 18:17:26 EST 2002
> From: Jeff Shannon [mailto:jeff at ccvcorp.com]
> Michal Wallace wrote:
>
> > On Thu, 31 Jan 2002, Delaney, Timothy wrote:
> >
> > > > From: Skip Montanaro [mailto:skip at pobox.com]
> > > > Tim> a.Klass -> _1__attr1 and _1__attr2
> > > > Tim> b.Klass -> _2__attr1 and _2__attr2
> > > >
> > > > This would be unworkable. Private attributes couldn't
> be pickled.
> > >
> > > Damn - knew there had to be *something* big against it.
> > >
> > > However, I can't help but feel that there must be *some*
> > > way to enforce that a "private" variable won't be
> > > accidentally used by a subclass.
> >
> > But that's exactly what the magical __whatever notation is
> > for int he first place! :)
>
> Well, yes, but the O.P. was providing an example of two classes in
> different modules with the same name, one of which inherits from the
> other. (i.e., class SomeClass(othermodule.SomeClass): ...)
>
> Of course, the solution to this problem is -- don't do that! :) It
> seems to me that it'd be confusing anyway. If nothing else, call the
> derived one My_SomeClass or something (or the base one
> BaseSomeClass), so
Ah - but my most important point was that sometimes you may not know the
know of one of the classes in the inheritance tree. Say I have a class
called Klass, and I want to create a subclass called TimKlass.
However, unbeknowst to me, the author of the class was *also* called Tim,
and Klass actually inherits from a TimKlass.
Suddenly you may find "private" attributes changing for no apparent reason.
Yes, I know - read the source. However, the whole point of a public API is
that it should not matter how things are composed. Perhaps a better example:
I inherit from Klass to create TimKlass, having ensured that there is no
class in the inheritance tree called TimKlass. I get a new version of the
library. Now Klass *does* inherit from TimKlass, and I happen to be using
the same private attribute names that TimKlass does. My code breaks *badly*
for no apparent reason - and hunting down the problem would be an absolute
nightmare.
Tim Delaney
More information about the Python-list
mailing list