P
pinkfloydhomer
Isn't there an easier way than
lst[len(lst) - 1] = ...
?
lst[len(lst) - 1] = ...
?
lst[-1] = ...Isn't there an easier way than
lst[len(lst) - 1] = ...
Peter said:No. You cannot overload assignment.
I knew there was an easy way
Just to satisfy my curiousity: Is there a way to do something like the
reference solution I suggest above?
{'foo': 42, 'bar': 'Ni!'}>>> lst = ['the', 'last', 'element of this list is a dict', {'key':"key's value"}]
>>> ref = lst[-1]
>>> ref {'key': "key's value"}
>>> ref["foo"] =42
>>> ref['bar'] = 'Ni!'
>>> ref {'foo': 42, 'bar': 'Ni!', 'key': "key's value"}
>>> ref['key'] "key's value"
>>> ref['bar'] 'Ni!'
>>> del ref['key']
>>> ref
But if lst[42]["pos"] happens to hold an integer value, then
a = lst[42]["pos"]
will _copy_ that integer value into 'a', right?
Changing 'a' will not
change the value at lst[42]["pos"]
Jerzy said:I have the impression that this is not an issue, to overload assignments,
which btw. *can* be overloaded, but the absence of *aliasing*
(undiscriminate handling of pointers) in Python. Am I wrong?
But if lst[42]["pos"] happens to hold an integer value, then
a = lst[42]["pos"]
will _copy_ that integer value into 'a', right? Changing 'a' will not
change the value at lst[42]["pos"]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 2>>> lst = range(42)+[{'pos':123}]+range(43,50)
>>> lst
49421860>>> lst[41:44] [41, {'pos': 123}, 43]
>>> lst[42] {'pos': 123}
>>> lst[42]['pos'] 123
>>> a = lst[42]['pos']
>>> a 123
>>> lst[42]['pos'] 123
>>> id(lst[42]['pos']) 49421860
>>> id(a)
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.
I think so.
a = b
will always make a a reference to (the same object as) b. What can be
overloaded is attribute assignment:
x.a = b
can do anything from creating an attribute that references b to wiping your
hard disk.
I don't understand what you mean by "absence of aliasing", but conceptually
every python variable is a -- well-behaved -- pointer.
Just to satisfy my curiousity: Is there a way to do something like the
reference solution I suggest above?
Jerzy said:Sure, I didn't want to claim that the assignment a=anything can be plainly
overloaded.
Would you please concentrate on - what I underlined - the sense of "C"
aliasing, where you can make a pointer to point to anything, say, the
176th byte of a function code?
This is impossible in Python.
Would you please concentrate on - what I underlined - the sense of "C"
aliasing,
where you can make a pointer to point to anything, say, the 176th byte
of a
function code?
([6], [6])a = [5]; b = a
a[0] = 6
a,b
causes a to point to a new list, containing the value 6, whereas the 2nd
I just want an alias.
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.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.