Addressing the last element of a list

2005-11-08 Thread [EMAIL PROTECTED]
Isn't there an easier way than lst[len(lst) - 1] = ... ? -- http://mail.python.org/mailman/listinfo/python-list

Re: Addressing the last element of a list

2005-11-08 Thread [EMAIL PROTECTED]
Or alternatively: Is there a way to make reference to the last element of a list, to use as a shorthand: ref := &lst[len(lst) - 1] # I know syntax is wrong, but you get the idea and then using the last element of the list by (assuming the element is a dict): ref["foo"] = 42 ref["bar"] = "Ni!"

Re: Addressing the last element of a list

2005-11-08 Thread [EMAIL PROTECTED]
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? -- http://mail.python.org/mailman/listinfo/python-list

Re: Addressing the last element of a list

2005-11-08 Thread Andreas Lobinger
Aloha, [EMAIL PROTECTED] wrote: > Isn't there an easier way than > lst[len(lst) - 1] = ... lst[-1] = ... Wishing a happy day LOBI -- http://mail.python.org/mailman/listinfo/python-list

Re: Addressing the last element of a list

2005-11-08 Thread Peter Otten
[EMAIL PROTECTED] wrote: > Just to satisfy my curiousity: Is there a way to do something like the > reference solution I suggest above? No. You cannot overload assignment. Of course, for mutable objects you can use the object as the "reference" to itself. Peter -- http://mail.python.org/mailm

Re: Addressing the last element of a list

2005-11-08 Thread [EMAIL PROTECTED]
what do you mean by alias ? a = b now both a and b refers to the same object. [EMAIL PROTECTED] wrote: > So there is no way in Python to make an alias for an object? > > /David -- http://mail.python.org/mailman/listinfo/python-list

Re: Addressing the last element of a list

2005-11-08 Thread [EMAIL PROTECTED]
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 shorthan

Re: Addressing the last element of a list

2005-11-08 Thread [EMAIL PROTECTED]
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"] -- http://mail.python.org/mailman/listinfo/python-list

Re: Addressing the last element of a list

2005-11-08 Thread Bengt Richter
On 8 Nov 2005 01:12:07 -0800, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: >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? > If the last element in the list was a dict, then you could do something li

Re: Addressing the last element of a list

2005-11-08 Thread Jerzy Karczmarczuk
Peter Otten wrote: > [EMAIL PROTECTED] wrote: > > >>Just to satisfy my curiousity: Is there a way to do something like the >>reference solution I suggest above? > > > No. You cannot overload assignment. I have the impression that this is not an issue, to overload assignments, which btw. *can*

Re: Addressing the last element of a list

2005-11-08 Thread Bengt Richter
On 8 Nov 2005 01:43:43 -0800, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: >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"] Right, but try an example:

Re: Addressing the last element of a list

2005-11-08 Thread Peter Otten
Jerzy Karczmarczuk wrote: > Peter Otten wrote: >> [EMAIL PROTECTED] wrote: >> >> >>>Just to satisfy my curiousity: Is there a way to do something like the >>>reference solution I suggest above? >> >> >> No. You cannot overload assignment. > > I have the impression that this is not an issue, t

Re: Addressing the last element of a list

2005-11-08 Thread [EMAIL PROTECTED]
If you want to do what you want(though I don't know why without a concrete example), just store a mutable object at lst[42]["pos"], like this : lst[42]["pos"] = [1] a = lst[42]["pos"] a[0] = 2 assert(lst[42]["pos"][0] == 2) [EMAIL PROTECTED] wrote: > But if lst[42]["pos"] happens to hold an inte

Re: Addressing the last element of a list

2005-11-08 Thread Simon Brunning
On 8 Nov 2005 01:43:43 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > But if lst[42]["pos"] happens to hold an integer value, then > > a = lst[42]["pos"] > > will _copy_ that integer value into 'a', right? Nope. It will bind the name 'a' to the integer object. > Changing 'a' will not > cha

Re: Addressing the last element of a list

2005-11-08 Thread Peter Otten
[EMAIL PROTECTED] 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 co

Re: Addressing the last element of a list

2005-11-08 Thread Jerzy Karczmarczuk
Peter Otten wrote cites me: >>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? > > > I think so. > > a = b > > will always make a a reference

Re: Addressing the last element of a list

2005-11-08 Thread [EMAIL PROTECTED]
So there is no way in Python to make an alias for an object? /David -- http://mail.python.org/mailman/listinfo/python-list

Re: Addressing the last element of a list

2005-11-08 Thread Matt Hammond
On Tue, 08 Nov 2005 10:25:21 -, Jerzy Karczmarczuk <[EMAIL PROTECTED]> wrote: > 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? Pretty much everything is r

Re: Addressing the last element of a list

2005-11-08 Thread Giovanni Bajo
[EMAIL PROTECTED] wrote: > I just want an alias. What you want is impossible. But there are many workarounds. > 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

Re: Addressing the last element of a list

2005-11-08 Thread Peter Otten
Jerzy Karczmarczuk wrote: > Sure, I didn't want to claim that the assignment a=anything can be plainly > overloaded. I interpreted your previous post as such a claim. No disagreement here then. > Would you please concentrate on - what I underlined - the sense of "C" > aliasing, where you can ma

Re: Addressing the last element of a list

2005-11-08 Thread Steven D'Aprano
On Tue, 08 Nov 2005 01:31:31 -0800, [EMAIL PROTECTED] wrote: > So there is no way in Python to make an alias for an object? Yes, sort of. Bind two names to the same mutable object: py> x = ["Something mutable"] py> y = x py> y.append("this way comes.") py> print x ['Something mutable', 'this way

Re: Addressing the last element of a list

2005-11-08 Thread Steven D'Aprano
On Tue, 08 Nov 2005 01:04:13 -0800, [EMAIL PROTECTED] wrote: > Or alternatively: > > Is there a way to make reference to the last element of a list, to use > as a shorthand: > > ref := &lst[len(lst) - 1] # I know syntax is wrong, but you get the > idea > > and then using the last element of the

Re: Addressing the last element of a list

2005-11-08 Thread Steven D'Aprano
On Tue, 08 Nov 2005 01:43:43 -0800, [EMAIL PROTECTED] wrote: > 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"] Not quite. Don't think of Python name

Re: Addressing the last element of a list

2005-11-08 Thread Michael
[EMAIL PROTECTED] wrote: > Is there a way to make reference to the last element of a list, to use > as a shorthand: Yes. It's not wise to use, since this is brittle and will fail hard for you, but since you're interested, this is how you /could/ do it: (largely) # First of all create a mechanism

Re: Addressing the last element of a list

2005-11-09 Thread Donn Cave
Quoth Steven D'Aprano <[EMAIL PROTECTED]>: ... | So when working with ints, strs or other immutable objects, you aren't | modifying the objects in place, you are rebinding the name to another | object: | | py> spam = "a tasty meat-like food" | py> alias = spam # both names point to the same str ob

Re: Addressing the last element of a list

2005-11-10 Thread Steven D'Aprano
On Thu, 10 Nov 2005 06:47:41 +, Donn Cave wrote: > Quoth Steven D'Aprano <[EMAIL PROTECTED]>: > ... > | So when working with ints, strs or other immutable objects, you aren't > | modifying the objects in place, you are rebinding the name to another > | object: > | > | py> spam = "a tasty meat-

Re: Addressing the last element of a list

2005-11-10 Thread Antoon Pardon
Op 2005-11-10, Steven D'Aprano schreef <[EMAIL PROTECTED]>: > On Thu, 10 Nov 2005 06:47:41 +, Donn Cave wrote: > >> Quoth Steven D'Aprano <[EMAIL PROTECTED]>: >> ... >> | So when working with ints, strs or other immutable objects, you aren't >> | modifying the objects in place, you are rebindin

Re: Addressing the last element of a list

2005-11-10 Thread Peter Otten
Antoon Pardon wrote: > Write somekind of property so that if you manipulate a.x it would > manipulate data[-1] Like that? >>> def make_prp(index): ... def get(self): return self[index] ... def set(self, value): self[index] = value ... return property(get, set) ... >>> class List(list

Re: Addressing the last element of a list

2005-11-10 Thread Daniel Crespo
Hi Proposition 1: > data = [0, None, 2, 0] > ref = data[-1] > ref = 1 > assert data[-1] == 1 Logically, it doesn't work. Here you are assigning to ref a NEW value. That won't replace the data[-1] value. It's out of logic this proposition. While this (proposition 2): data = [0, None, 2, ["hello"]

Re: Addressing the last element of a list

2005-11-10 Thread [EMAIL PROTECTED]
This mutable/immutable object and name/variable is confusing. Daniel Crespo wrote: > Well, I hope that newcomers to Python don't confuse himselves :) > > Daniel -- http://mail.python.org/mailman/listinfo/python-list

Re: Addressing the last element of a list

2005-11-10 Thread Mike Meyer
[Context recovered from top posting.] "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > Daniel Crespo wrote: >> Well, I hope that newcomers to Python don't confuse himselves :) > This mutable/immutable object and name/variable is confusing. Only if you have to overcome a conviction that variables

Re: Addressing the last element of a list

2005-11-10 Thread [EMAIL PROTECTED]
After one knows the characteristic, it is no problem. But judging from the frequency of the same issue coming up from time to time, it does confuse people coming from certain programming background. Mike Meyer wrote: > Only if you have to overcome a conviction that variables behave in a > differen

Re: Addressing the last element of a list

2005-11-10 Thread Donn Cave
In article <[EMAIL PROTECTED]>, Mike Meyer <[EMAIL PROTECTED]> wrote: ... > Most OO languages do the name/variable thing, but some of the popular > ones aren't consistent about it, giving some types "special" status, > so that sometimes "a = b" causes b to be copied onto a, and sometimes > it caus

Re: Addressing the last element of a list

2005-11-10 Thread Steven D'Aprano
On Thu, 10 Nov 2005 08:19:36 -0800, [EMAIL PROTECTED] wrote: > This mutable/immutable object and name/variable is confusing. It is a source of confusion to newbies, because it is a distinction that may not be intuitively obvious, and it is a programming model that isn't quite the same as many peo

Re: Addressing the last element of a list

2005-11-14 Thread Antoon Pardon
Op 2005-11-10, Mike Meyer schreef <[EMAIL PROTECTED]>: > [Context recovered from top posting.] > > "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: >> Daniel Crespo wrote: >>> Well, I hope that newcomers to Python don't confuse himselves :) >> This mutable/immutable object and name/variable is confu

Re: Addressing the last element of a list

2005-11-14 Thread Paul Rubin
Antoon Pardon <[EMAIL PROTECTED]> writes: > We could then have something like the following. > > a = 5 > b = a > a @= 7 > b ==> would result in 7. Ouch! :-((( Can't you live with a = [5] b = a a[0] = 7 so b[0] is now 7. -- http://mail.python.org/mailman/listinfo/python-list

Re: Addressing the last element of a list

2005-11-14 Thread Antoon Pardon
Op 2005-11-14, Paul Rubin schreef : > Antoon Pardon <[EMAIL PROTECTED]> writes: >> We could then have something like the following. >> >> a = 5 >> b = a >> a @= 7 >> b ==> would result in 7. > > Ouch! :-((( > > Can't you live with > > a = [5] > b = a > a[0] = 7 > > so b[0] is now 7. And what do

Re: Addressing the last element of a list

2005-11-14 Thread Mike Meyer
Antoon Pardon <[EMAIL PROTECTED]> writes: > Op 2005-11-10, Mike Meyer schreef <[EMAIL PROTECTED]>: >> [Context recovered from top posting.] >> "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: >>> Daniel Crespo wrote: Well, I hope that newcomers to Python don't confuse himselves :) >>> This muta

Re: Addressing the last element of a list

2005-11-14 Thread Bengt Richter
On 14 Nov 2005 11:20:53 GMT, Antoon Pardon <[EMAIL PROTECTED]> wrote: >Op 2005-11-14, Paul Rubin schreef : >> Antoon Pardon <[EMAIL PROTECTED]> writes: >>> We could then have something like the following. >>> >>> a = 5 >>> b = a >>> a @= 7 >>> b ==> would result in 7. >> >> Ouch! :-((( >> >> Can

Re: Addressing the last element of a list

2005-11-14 Thread [EMAIL PROTECTED]
Bengt Richter wrote: > You may be interested in reviewing > > > http://groups.google.com/group/comp.lang.python/browse_thread/thread/f96b496b6ef14e2/32d3539e928986b3 > > before continuing this topic ;-) Interesting indeed, I mean the argument of why python does it this way. I see the equiva

Re: Addressing the last element of a list

2005-11-14 Thread Bengt Richter
On Mon, 14 Nov 2005 09:53:34 -0500, Mike Meyer <[EMAIL PROTECTED]> wrote: >Antoon Pardon <[EMAIL PROTECTED]> writes: >> Op 2005-11-10, Mike Meyer schreef <[EMAIL PROTECTED]>: >>> [Context recovered from top posting.] >>> "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: Daniel Crespo wrote: >>>

Re: Addressing the last element of a list

2005-11-15 Thread Antoon Pardon
Op 2005-11-14, Mike Meyer schreef <[EMAIL PROTECTED]>: > Antoon Pardon <[EMAIL PROTECTED]> writes: >> Op 2005-11-10, Mike Meyer schreef <[EMAIL PROTECTED]>: >>> [Context recovered from top posting.] >>> "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: Daniel Crespo wrote: > Well, I hope tha

Re: Addressing the last element of a list

2005-11-15 Thread Antoon Pardon
Op 2005-11-14, Bengt Richter schreef <[EMAIL PROTECTED]>: > On 14 Nov 2005 11:20:53 GMT, Antoon Pardon <[EMAIL PROTECTED]> wrote: > >>Op 2005-11-14, Paul Rubin schreef : >>> Antoon Pardon <[EMAIL PROTECTED]> writes: We could then have something like the following. a = 5 b = a >

Re: Addressing the last element of a list

2005-11-15 Thread Chris Mellon
On 11/14/05, Bengt Richter <[EMAIL PROTECTED]> wrote: > On 14 Nov 2005 11:20:53 GMT, Antoon Pardon <[EMAIL PROTECTED]> wrote: > > >Op 2005-11-14, Paul Rubin schreef : > >> Antoon Pardon <[EMAIL PROTECTED]> writes: > >>> We could then have something like the following. > >>> > >>> a = 5 > >>> b = a

Re: Addressing the last element of a list

2005-11-15 Thread Bengt Richter
On 15 Nov 2005 08:51:59 GMT, Antoon Pardon <[EMAIL PROTECTED]> wrote: >Op 2005-11-14, Bengt Richter schreef <[EMAIL PROTECTED]>: [...] >> You may be interested in reviewing >> >> >> http://groups.google.com/group/comp.lang.python/browse_thread/thread/f96b496b6ef14e2/32d3539e928986b3 >> >> bef

Re: Addressing the last element of a list

2005-11-15 Thread Bengt Richter
On Tue, 15 Nov 2005 03:23:11 -0600, Chris Mellon <[EMAIL PROTECTED]> wrote: >On 11/14/05, Bengt Richter <[EMAIL PROTECTED]> wrote: [...] >> You may be interested in reviewing >> >> http://groups.google.com/group/comp.lang.python/browse_thread/thread/= >f96b496b6ef14e2/32d3539e928986b3 >> >> be

Re: Addressing the last element of a list

2005-11-15 Thread Mike Meyer
Antoon Pardon <[EMAIL PROTECTED]> writes: >>> Like having an assignment operator (let use @= for it) next to a >>> (re)bind operator. >>> We could then have something like the following. >>> a = 5 >>> b = a >>> a @= 7 >>> b ==> would result in 7. >> You've just overwritten the object referred to by

Re: Addressing the last element of a list

2005-11-16 Thread Antoon Pardon
Op 2005-11-15, Bengt Richter schreef <[EMAIL PROTECTED]>: > On 15 Nov 2005 08:51:59 GMT, Antoon Pardon <[EMAIL PROTECTED]> wrote: > >>Op 2005-11-14, Bengt Richter schreef <[EMAIL PROTECTED]>: > [...] >>> You may be interested in reviewing >>> >>> >>> http://groups.google.com/group/comp.lang.py

Re: Addressing the last element of a list

2005-11-16 Thread Antoon Pardon
Op 2005-11-15, Mike Meyer schreef <[EMAIL PROTECTED]>: > Antoon Pardon <[EMAIL PROTECTED]> writes: Like having an assignment operator (let use @= for it) next to a (re)bind operator. We could then have something like the following. a = 5 b = a a @= 7 b ==> would r

Re: Addressing the last element of a list

2005-11-16 Thread Mike Meyer
Antoon Pardon <[EMAIL PROTECTED]> writes: > Op 2005-11-15, Mike Meyer schreef <[EMAIL PROTECTED]>: >> Antoon Pardon <[EMAIL PROTECTED]> writes: > Like having an assignment operator (let use @= for it) next to a > (re)bind operator. > We could then have something like the following.

Re: Addressing the last element of a list

2005-11-17 Thread Duncan Booth
Mike Meyer wrote: > Wouldn't one.__dict__ = dict(**two.__dict__) be a bit better? > Those stars are redundant here. It would be cleaner to write: one.__dict__ = dict(two.__dict__) -- http://mail.python.org/mailman/listinfo/python-list

Re: Addressing the last element of a list

2005-11-17 Thread Antoon Pardon
Op 2005-11-16, Mike Meyer schreef <[EMAIL PROTECTED]>: > Antoon Pardon <[EMAIL PROTECTED]> writes: >> Op 2005-11-15, Mike Meyer schreef <[EMAIL PROTECTED]>: >>> Antoon Pardon <[EMAIL PROTECTED]> writes: >> Like having an assignment operator (let use @= for it) next to a >> (re)bind operator