Addressing the last element of a list

Peter Otten __peter__ at web.de
Tue Nov 8 05:21:01 EST 2005


pinkfloydhomer at gmail.com wrote:

> I just want an alias. Ideally, I don't want to deal with pointers or
> special reference "types" etc. After all, this is not C++ :)
> 
> I just want to be able to make a reference to any old thing in Python.
> A list, an integer variable, a function etc. so that I, in complicated
> cases, can make a shorthand. If "myRef" could somehow "point at"
> something long an complicated like a[42]["pos"][-4], that might be
> useful.

You can do

shorthand = a[42]["pos"]

...

print shorthand[-4]
shorthand[0] = 42 # if a[42]["pos"] is mutable

But the last indirection must always be given explicitly.

Peter




More information about the Python-list mailing list