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


Groups > comp.lang.python > #38391

Re: len() on mutables vs. immutables

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!news2.euro.net!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'cache': 0.05; 'context': 0.05; 'cpython': 0.05; 'report.': 0.07; 'python': 0.09; "(i'd": 0.09; 'behavior,': 0.09; 'immutable': 0.09; 'incorrect': 0.09; 'internally': 0.09; 'mutable': 0.09; 'url:github': 0.09; 'assume': 0.11; 'do,': 0.15; 'passing': 0.15; 'source,': 0.15; '"terry': 0.16; "(i'm": 0.16; '(more': 0.16; '(well,': 0.16; '>on': 0.16; '__len__,': 0.16; 'anyway).': 0.16; 'awesome.': 0.16; 'call?': 0.16; 'dig': 0.16; 'mundane': 0.16; 'reedy': 0.16; 'reedy"': 0.16; 'stuff,': 0.16; 'wrote:': 0.17; 'shape': 0.17; 'thanks,': 0.18; 'jan': 0.18; 'obviously': 0.18; 'memory': 0.18; 'assuming': 0.22; 'controlled': 0.22; 'operations.': 0.22; 'url:02': 0.22; "i'd": 0.22; "python's": 0.23; 'to:2**1': 0.23; "i've": 0.23; 'seems': 0.23; 'least': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'done.': 0.27; 'question': 0.27; '(such': 0.27; 'post': 0.28; 'overhead': 0.29; 'questions.': 0.29; 'url:mailman': 0.29; 'reporting': 0.29; 'source': 0.29; "i'm": 0.29; 'function': 0.30; 'point': 0.31; 'asking': 0.32; 'implement': 0.32; 'url:python': 0.32; 'url:listinfo': 0.32; 'curious': 0.33; 'hopefully': 0.33; 'anyone': 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; 'pm,': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'message-id:@gmail.com': 0.36; 'url:org': 0.36; "i'll": 0.36; 'charset:us-ascii': 0.36; 'rather': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'little': 0.39; 'help': 0.40; 'url:mail': 0.40; 'bottom': 0.60; 'side': 0.61; 'safe': 0.63; 'skip:n 10': 0.63; 'confirm': 0.64; 'here': 0.65; 'results': 0.65; 'subject:. ': 0.66; 'eyes': 0.69; 'insight': 0.71; 'calculations': 0.84; 'url:2013': 0.84; 'url:posts': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:user-agent:date:subject:from:to:message-id:thread-topic :in-reply-to:mime-version:content-type:content-transfer-encoding; bh=FjdCD9XcDPwIurUxr+oDXYTZoZA89KApyjgBYhIzxZ4=; b=oCaKU33sU7ifkolJQK+AAk3x5SIpuptk/c1TPTjhubV9K2rmOa3BMBraLsxSr325iB rxjWpJPJI1AP8haNyLxaOlGcT7YsTfcItv31knowxaIFg9gyH5fubWUYYZO0/BVhFHBU DaIei/32stfW6ZPNoVgw20YmdnQTLskFRVIbGDW4mMaS0OCBy/z/kt52/f+3qJmh+RGU 0jqxGTvayMRE6ECyyWSfLH5tjCVvhHeGaq9gz1A7Qk+siyDpe/95VESEyHUv6/tpjryT fDOPTBNNCp1eENaDr8Lyt8Z91/MMJV7i/gkNY4MusxhFUj47ckvJhiHGiuibCEdx4nat 3x+w==
X-Received by 10.50.149.168 with SMTP id ub8mr7006072igb.111.1360285779010; Thu, 07 Feb 2013 17:09:39 -0800 (PST)
User-Agent Microsoft-MacOutlook/14.3.0.121105
Date Thu, 07 Feb 2013 17:09:31 -0800
Subject Re: len() on mutables vs. immutables
From Demian Brecht <demianbrecht@gmail.com>
To Terry Reedy <tjreedy@udel.edu>, <python-list@python.org>
Thread-Topic len() on mutables vs. immutables
In-Reply-To <k5qa8u$bb5$1@ger.gmane.org>
Mime-version 1.0
Content-type text/plain; charset="US-ASCII"
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.1465.1360285781.2939.python-list@python.org> (permalink)
Lines 65
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360285781 news.xs4all.nl 6938 [2001:888:2000:d::a6]:41603
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38391

Show key headers only | View raw


So, it's taken me a little while longer than I figured to actually get the
time to dig around for the question that I had (added to the bottom of
this message for context).. Pretty mundane stuff, but I did the digging
(3.4.0a). Hopefully the results will help anyone else with the same
questions.

http://demianbrecht.github.com/posts/2013/02/07/understanding-len/


However, my research brought up a question (I'm assuming someone here can
answer this):

If a memoryview is representing a non-continuguous block of memory (> 1
ndim), will len(obj) not return incorrect results? It seems to be
reporting the shape of the 0th dim at the moment.. Or is there something
that I'm missing altogether?

Thanks,

Demian Brecht
http://demianbrecht.github.com



On 2012-10-18 5:26 PM, "Terry Reedy" <tjreedy@udel.edu> wrote:

>On 10/18/2012 2:42 PM, Demian Brecht wrote:
>
>> Awesome. Pretty much what I figured. Of course, I'll have to dig around
>> the source just to confirm this with my own eyes (more just curiosity
>> than anything),
>
>If you do, please followup with a report.
>
>-- 
>Terry Jan Reedy
>
>-- 
>http://mail.python.org/mailman/listinfo/python-list

** Quote for context on a necro'd post **

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

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


Thread

Re: len() on mutables vs. immutables Demian Brecht <demianbrecht@gmail.com> - 2013-02-07 17:09 -0800

csiph-web