[C++-sig] Re: Support for Indexing and Iteration Safety

Mike Rovner mike at bindkey.com
Fri May 30 00:41:21 CEST 2003


"David Abrahams" <dave at boost-consulting.com> wrote in message
news:u4r3dxvk1.fsf at boost->
> >>            >>> x = foo[10].bar
> >>            >>> del foo[10]  # erase the 10th item
> >>            >>> print x
> >
> > What about having x a weak reference which del will reset to None.
> I considered that, but is that really Pythonic and unsurprising?

IMHO yes

> Anyway, it's not much easier to make work than the one which copies
> the referenced object when its position is invalidated.

Instead of making x a copy on the fly? Refusing to del?
I think both of them less pythonic.

> >>            >>> x = foo[10]
> >>            >>> foo[10] = y
> >>            >>> print x, y
> >>
> >>         Now you'll see that x and y are always identical, because x
> >>         refers to the place in the array where you've written the
> >>         value of y.
> >
> > It's ok, because Python have explicit copy semantics
> > The list isn't the only model.
>
> Huh?  How does "explicit copy semantics" change anything?
> What does this have to do with "the list"?

I mean foo[10] is not (foo.as_list())[10]
if I assume foo[10] ia an object, then
x=foo[10] is making a reference to it (in python) like x=a
Then a=y will make x and a both reference to y.

Well, it doesn't sound clear even to me :) Sorry.
I'm trying to tell than foo[10] may be considered not a 10th element of LIST
foo
and in that case asking for explicit copy is wholeheart pythonic.

> >>      4. Similar problem with exposed iterators:
> >>          change out from under you.  The above could lead to a crash
> >>          or bogus results.
> >
> > bogus results are ok because it's the same as in Python - changing a
list
> > under loop can lead to skiping an element or visiting it twice.
>
> Yes, but it will never crash.

> > I think if it will put some restriction on usage it is ok with me.
>
> Do you mean to say that it's OK to expose the user to crashes as long
> as we tell him what he's allowed to do, and only crash if he disobeys?

Yes if that will not be the default, but explicitly asked for (for speed
reasons may be).

Regards,
Mike







More information about the Cplusplus-sig mailing list