"Private" attributes - a possible idea (maybe PEP-worthy).

Jeff Shannon jeff at ccvcorp.com
Thu Jan 31 16:19:31 EST 2002


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
that it is clear *which* module's SomeClass you're working with, at a
glance.  The fact that using the same classname also whacks the
name-mangling is just another indication that this is a confusing bad
idea.

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list