[Python-Dev] PEP 246: lossless and stateless
Just van Rossum
just at letterror.com
Sat Jan 15 10:39:03 CET 2005
Phillip J. Eby wrote:
> At 07:02 PM 1/14/05 -0500, Glyph Lefkowitz wrote:
> >For the sake of argument, let's say that SegmentPen is a C type,
> >which does not have a __dict__, and that PointPen is a Python
> >adapter for it, in a different project.
>
> There are multiple implementation alternatives possible here; it
> isn't necessary that the state be hidden there. The point is that,
> given the same SegmentPen, we want to get the same PointPen each time
> we *implicitly* adapt, in order to avoid violating the "naive"
> developer's mental model of what adaptation is -- i.e. an extension
> of the object's state, not a new object with independent state.
>
> One possible alternative implementation is to use a dictionary from
> object id to a 'weakref(ob),state' tuple, with the weakref set up to
> remove the entry when 'ob' goes away. Adapters would then have a
> pointer to their state object and a pointer to the adaptee. As long
> as an adapter lives, the adaptee lives, so the state remains valid.
> Or, if no adapters remain, but the adaptee still lives, then so does
> the state which can be resurrected when a new adapter is requested.
> It's too bad Python doesn't have some sort of deallocation hook you
> could use to get notified when an object goes away. Oh well.
That sounds extremely complicated as apposed to just storing the sate
where it most logically belongs: on the adapter. And all that to work
around a problem that I'm not convinced needs solving or even exists. At
the very least *I* don't care about it in my use case.
> Anyway, as you and I have both pointed out, sticky adaptation is an
> important use case; when you need it, you really need it.
Maybe I missed it, but was there an example posted of when "sticky
adaptation" is needed?
It's not at all clear to me that "sticky" behavior is the best default
behavior, even with implicit adoptation. Would anyone in their right
mind expect the following to return [0, 1, 2, 3, 4, 5] instead of [0, 1,
2, 0, 1, 2]?
>>> from itertools import *
>>> seq = range(10)
>>> list(chain(islice(seq, 3), islice(seq, 3)))
[0, 1, 2, 0, 1, 2]
>>>
Just
More information about the Python-Dev
mailing list