Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #6345

Re: Puzzled by list-appending behavior

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'newbie': 0.03; 'mrab': 0.05; 'url:bugs': 0.05; 'terry': 0.07; 'builtin': 0.09; 'collections': 0.09; 'convention,': 0.09; 'none.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'return,': 0.09; 'rule.': 0.09; 'am,': 0.14; 'wrote:': 0.14; 'defined': 0.14; 'docstring': 0.16; 'documented.': 0.16; 'item)': 0.16; 'rebert': 0.16; 'reedy': 0.16; 'return.': 0.16; 'subject:behavior': 0.16; 'surprising': 0.16; 'subject:list': 0.19; 'jan': 0.20; 'header:In- Reply-To:1': 0.21; 'similarly': 0.23; 'welcome.': 0.23; "doesn't": 0.25; 'point,': 0.25; "i'm": 0.27; 'explicitly': 0.29; 'equivalent': 0.31; 'header:X-Complaints-To:1': 0.32; 'to:addr :python-list': 0.33; 'post': 0.33; 'list.': 0.33; 'rather': 0.34; 'chris': 0.34; 'rule': 0.34; 'example,': 0.35; 'there': 0.35; 'header:User-Agent:1': 0.35; 'surprised': 0.35; 'none': 0.37; 'another': 0.37; 'think': 0.38; 'url:python': 0.38; 'received:org': 0.38; 'url:org': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'comments': 0.39; 'sometimes': 0.39; 'should': 0.39; "i'd": 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'proposal': 0.66; 'special': 0.66; 'collection': 0.72; '11:58': 0.84; 'proposals.': 0.84; 'confusion.': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Puzzled by list-appending behavior
Date Thu, 26 May 2011 17:36:22 -0400
References <16c21256-48df-416a-971f-de49ca4cc981@x6g2000yqj.googlegroups.com> <BANLkTik+M=MBHSBoRmXjLYVN8rvD515CBQ@mail.gmail.com> <4DDE78A1.9070305@mrabarnett.plus.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host rain.gmane.org
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10
In-Reply-To <4DDE78A1.9070305@mrabarnett.plus.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2134.1306445799.9059.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 82.94.164.166
X-Trace 1306445799 news.xs4all.nl 49044 [::ffff:82.94.164.166]:51184
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:6345

Show key headers only | View raw


On 5/26/2011 11:58 AM, MRAB wrote:
> On 26/05/2011 06:17, Chris Rebert wrote:
>> list.remove(), list.sort(), and list.extend() similarly return None
>> rather than the now-modified list.

> I'd just like to point out that it's a convention, not a rigid rule.
> Sometimes it's not followed, for example, dict.setdefault.

The rule for builtin collections is that mutation methods do not return 
the collection. If there is a member of the collection to return, they 
return that. Otherwise they return None.

list/set.pop and dict.popitem are other mutation methods that have a 
(former) member to return.

The rule applies to special methods like __getitem__ (returns an item) 
and __setitem__ (returns None). Since a.append(item) is *conceptually* 
equivalent to a.__setitem(len(a), item) (I know, it raises) and 
*actually* defined as a.__setitem(len(a):len(a), item), it should not be 
surprising that all three return None.

I think the above should be better documented.
http://bugs.python.org/issue12192
has some proposals. Comments there welcome.

In another post
On 5/26/2011 4:09 AM, Chris Rebert wrote:
 > I'm just surprised that the docstring doesn't explicitly state
 > "Returns None." by this point, given that this is such a common point
 > of newbie confusion.

I never noticed. After reading the above, I added this to the proposal 
above.



-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Puzzled by list-appending behavior Uncle Ben <bgreen@nycap.rr.com> - 2011-05-25 21:46 -0700
  Re: Puzzled by list-appending behavior Ben Finney <ben+python@benfinney.id.au> - 2011-05-26 15:11 +1000
  Re: Puzzled by list-appending behavior Chris Rebert <clp2@rebertia.com> - 2011-05-25 22:17 -0700
  Re: Puzzled by list-appending behavior Chris Angelico <rosuav@gmail.com> - 2011-05-26 17:20 +1000
  Re: Puzzled by list-appending behavior Chris Angelico <rosuav@gmail.com> - 2011-05-26 17:23 +1000
  Re: Puzzled by list-appending behavior Uncle Ben <bgreen@nycap.rr.com> - 2011-05-26 00:33 -0700
  Re: Puzzled by list-appending behavior Chris Rebert <clp2@rebertia.com> - 2011-05-26 01:09 -0700
  Re: Puzzled by list-appending behavior MRAB <python@mrabarnett.plus.com> - 2011-05-26 16:58 +0100
    Re: Puzzled by list-appending behavior Tim Roberts <timr@probo.com> - 2011-05-26 23:34 -0700
      Re: Puzzled by list-appending behavior MRAB <python@mrabarnett.plus.com> - 2011-05-27 17:02 +0100
  Re: Puzzled by list-appending behavior Chris Angelico <rosuav@gmail.com> - 2011-05-27 02:04 +1000
  Re: Puzzled by list-appending behavior John Ladasky <ladasky@my-deja.com> - 2011-05-26 11:27 -0700
    Re: Puzzled by list-appending behavior Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-27 01:59 +0000
      Re: Puzzled by list-appending behavior Chris Angelico <rosuav@gmail.com> - 2011-05-27 13:24 +1000
        Re: Puzzled by list-appending behavior Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-27 03:52 +0000
          Re: Puzzled by list-appending behavior Chris Angelico <rosuav@gmail.com> - 2011-05-27 14:10 +1000
          RE: Puzzled by list-appending behavior "Prasad, Ramit" <ramit.prasad@jpmchase.com> - 2011-05-27 13:58 -0400
          Re: Puzzled by list-appending behavior Ethan Furman <ethan@stoneleaf.us> - 2011-05-27 11:56 -0700
  Re: Puzzled by list-appending behavior Terry Reedy <tjreedy@udel.edu> - 2011-05-26 14:44 -0400
  RE: Puzzled by list-appending behavior "Prasad, Ramit" <ramit.prasad@jpmchase.com> - 2011-05-26 15:34 -0400
  Re: Puzzled by list-appending behavior Terry Reedy <tjreedy@udel.edu> - 2011-05-26 17:36 -0400

csiph-web