anything like C++ references?

Donn Cave donn at u.washington.edu
Mon Jul 14 15:07:48 EDT 2003


In article <beus18$59q$0 at 216.39.172.122>, bokr at oz.net (Bengt Richter) 
wrote:
> On Mon, 14 Jul 2003 03:40:05 -0000, "Donn Cave" <donn at drizzle.com> wrote:
...
> Which might turn out to be equivalent to object.__setattr__(obj, 'x', 10)
> or obj.__setattribute__('x', 10) or obj.__class__.__dict__['x'].fset(obj, 10) 
> or ...
> 
> It depends on what you mean by 'targets'. I argued that the three left hand
> expressions all *ultimately* result in some machine level pointer variable 
> being set
> in some object's internal representation to point to the representation of 
> the object
> resulting from evaluating the right hand side, and in that sense all the 
> assignments
> have the same ultimate semantics. Some pointer in some composite object is 
> set to point
> to another object.
>
> Depending on the left hand object actually involved, we use different 
> expressions to
> retrieve the pointer (and often you have a choice, e.g., x.y or 
> x.__dict__['y'] or getattr(x,'y')
> or vars(x).get('y') etc., some of which might not be valid. Even plain x 
> might sometimes be
> retrieved by sys._getframe().f_locals.get('x').

My point really was that we can't express "pointer" or "target" in
Python, so Python can't "retrieve the pointer" from user defined
__setitem__ - it just has to hand it the values and assume that
an assignment will actually happen somewhere.

Not that it's worth making the necessary changes to Python, but it
does seem to me that it would have been more elegant and rigorous
to have indexing and assignment work that way, and it seems to me
that when we can't do it because we can't express "target", that
is a structural weakness in the language.

The Loc class you proposed as a solution doesn't really do that
at all - it's just a round-about way to call __setitem__, for all
I can tell.


> Or you could say the implementation is internal and you have
> to use the right spellings to effect the semantics indirectly.
> I.e., op[i]=newitem spells *p = newitem internally (names obviously not 
> directly related):
> 
> int
> PyList_SetItem(register PyObject *op, register int i,
>                register PyObject *newitem)
> {
>     ...
>     p = ((PyListObject *)op) -> ob_item + i;
>     olditem = *p;
>     *p = newitem;
>     Py_XDECREF(olditem);
>     return 0;
> }

I'm missing something here - this evidently does not apply to user
defined __setitem__?

   Donn Cave, donn at u.washington.edu




More information about the Python-list mailing list