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


Groups > comp.lang.python > #31667

len() on mutables vs. immutables

Path csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <demianbrecht@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'cache': 0.05; 'cpython': 0.05; 'python': 0.09; "(i'd": 0.09; 'behavior,': 0.09; 'immutable': 0.09; 'internally': 0.09; 'mutable': 0.09; 'url:github': 0.09; 'assume': 0.11; 'passing': 0.15; 'source,': 0.15; '(well,': 0.16; '__len__,': 0.16; 'anyway).': 0.16; 'call?': 0.16; 'dig': 0.16; 'thanks,': 0.18; 'obviously': 0.18; 'to:name :python-list@python.org': 0.20; 'controlled': 0.22; 'operations.': 0.22; "i'd": 0.22; "python's": 0.23; "i've": 0.23; 'least': 0.25; 'header:User-Agent:1': 0.26; 'done.': 0.27; '(such': 0.27; 'skip:@ 10': 0.27; 'overhead': 0.29; "i'm": 0.29; 'function': 0.30; 'point': 0.31; 'asking': 0.32; 'implement': 0.32; 'curious': 0.33; 'to:addr:python-list': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'built-in': 0.35; 'so,': 0.35; 'doing': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'message-id:@gmail.com': 0.36; 'rather': 0.37; 'received:209': 0.37; 'some': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'side': 0.61; 'safe': 0.63; 'subject:. ': 0.66; 'insight': 0.71; 'calculations': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=lh5Glkjt9OonHbystnSUxT511tucCvBPsQS1XA2sZw8=; b=iAp6izsVA65vBXajyiASR7ALO5JiZD2Vo7OxIgw078mUk/R9dDP4CZXL3/35cn/TaZ mdgV1GKKZZ4SHSCOYWUXaqh0eIvkeUwO7hH6+muW9NZEq5H0PXTki27+HYD3Gcx/szdR pJ8pNDB4Vm26FYWTopo3qbbTEbGhSl4q6iXLEiqjf4CtqOCby9WucF7QVVUb7gs/GsKd qr4LLJ82MhSFUiblK5ssRhi8kKj3Vs2fKyPtDZcG9NMnEq27gq4mPwpt3cP09STdQxE3 U29P5BzQXERNuhBoqh/oosWkGakoe7vv6PXzSqt9C0uHf5yES7gHrfxEG2OxWBjlda8y wkkg==
Date Thu, 18 Oct 2012 10:23:56 -0700
From Demian Brecht <demianbrecht@gmail.com>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1
MIME-Version 1.0
To "python-list@python.org" <python-list@python.org>
Subject len() on mutables vs. immutables
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.2457.1350581045.27098.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350581045 news.xs4all.nl 6884 [2001:888:2000:d::a6]:42139
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31667

Show key headers only | View raw


I'm curious as to the implementation (I'd be happy to dig through the 
source, just don't have the time right now). I've seen various 
implementations across interpreters in the past (some which have been 
rather shocking) and I'd like to get some insight into Python (well, 
CPython at this point anyway).

When len() is called passing an immutable built-in type (such as a 
string), I'd assume that the overhead in doing so is simply a function 
call and there are no on-call calculations done. Is that correct?

I'd also assume that mutable built-in types (such as a bytearray) would 
cache their size internally as a side effect of mutation operations. Is 
that correct? If so, is it safe to assume that at least all built-in 
types observe this behavior, or are there some that incur an O(n) cost 
on every len() call?

Obviously this can't be controlled with custom types that implement 
their own __len__, I'm only asking about Python's built-ins.

Thanks,
Demian
@demianbrecht
http://demianbrecht.github.com

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


Thread

len() on mutables vs. immutables Demian Brecht <demianbrecht@gmail.com> - 2012-10-18 10:23 -0700

csiph-web