append on lists
Lie
Lie.1296 at gmail.com
Wed Sep 17 11:51:08 EDT 2008
On Sep 16, 3:20 pm, Armin <a... at nospam.org> wrote:
> John Machin wrote:
> > On Sep 16, 6:45 am, Armin <a... at nospam.org> wrote:
>
> >> Yes, but this is very unconvenient.
> >> If d should reference the list a extended with a single list element
> >> you need at least two lines
>
> >> a.append(7)
> >> d=a
>
> >> and not more intuitive d = a.append(7)
>
> > Methods/functions which return a value other than the formal None and
> > also mutate their environment are "a snare and a delusion". Don't wish
> > for them.
>
> c = [9,10]
> [1,2,3,4,7].append(c) -> Is this a valid expression?
>
Yes, that is a valid expression, however, the list you're appending to
is immediately discarded.
> The 'value' of that expression is None.
>
> However ... that's the way of the implementation of the append method.
> It's a little bit confusing to me ...
actually, you could implement your own myList which returns a value on
append method:
class MyList(list):
def append(self, item):
list.append(self, item)
return self
# or
# return self[:]
# if you want it to return a copy
But I recommend against that.
> --Armin
>
> Thanks to all !
>
>
>
>
>
> > Inconvenient? How often do you want to mutate a list and then set up
> > another reference to it?- Hide quoted text -
>
> - Show quoted text -
More information about the Python-list
mailing list