[Python-Dev] Meta-reflections

Kevin Jacobs jacobs@penguin.theopalgroup.com
Wed, 20 Feb 2002 14:49:43 -0500 (EST)


On Wed, 20 Feb 2002, David Ascher wrote:
> Kevin Jacobs wrote:
> This example is not a great example of that, since the code above does
> exactly the same thing if you delete the lines defining __slots__.

True, which is why the current implementation (IMHO) isn't broken; just
flawed.  There is, in effect, a flat slot namespace, only by virtue of the
fact that there is no simple and explicit slot resolution syntax.  This
basically means that most arguments for using the current implementation of
slots as a data-hiding mechanism over inheritance are very weak unless
significant additional syntax is created.

> You're modifying class attributes in that case, but I think it's
> important to keep the examples which illustrate the problem "pure" and
> "realistic".

Nope -- they aren't class attributes at all, they are per-instance slots
with class-level descriptors (with which you expose another bug below).

> My take on this thread is that I think it's simply not going to happen
> that slots are going to act 100% like attributes except for
> performance/memory considerations.  It would be nice, but if that had
> been possible, then they'd simply be an internal optimization and would
> have no syntactic impact.

I'd like to know why else you think that?  I'm fairly confident that I can
submit a patch that accomplishes this (and even fix the following issue).

> There are much more shallow ways in which slots aren't like attributes:
>
> >>> class A(object):
> ...   __slots__ = ('a',)
> ...
> >>> a = A()
> >>> a.a = 123             # set a slot on a
> >>> A.a = 555             # set a slot on A
> >>> a.a                   # Way 1: A's slot overrides a's
> 555
> >>> b = A()
> >>> b.a
> 555
> >>> del A.a
> >>> a.a                   # Way 2: deleting the class slot
>                           # did not uncover the instance slot
> AttributeError: 'A' object has no attribute 'a'

Ouch!  You've discovered another problem with the current implementation.
You have effectively removed the slot descriptor from class A and replaced
it with a class attribute.  In fact, I don't think you can ever re-create
the slot descriptor!  This is actually the best form of data hiding in pure
Python I've seen to date.  The fix is to make slot descriptors read-only,
like the rest of the immutible class attributes.

Sigh,
-Kevin

--
Kevin Jacobs
The OPAL Group - Enterprise Systems Architect
Voice: (216) 986-0710 x 19         E-mail: jacobs@theopalgroup.com
Fax:   (216) 986-0714              WWW:    http://www.theopalgroup.com