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


Groups > comp.lang.python > #31680 > unrolled thread

RE: len() on mutables vs. immutables

Started by"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
First post2012-10-18 19:18 +0000
Last post2012-10-18 19:18 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  RE: len() on mutables vs. immutables "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-18 19:18 +0000

#31680 — RE: len() on mutables vs. immutables

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Date2012-10-18 19:18 +0000
SubjectRE: len() on mutables vs. immutables
Message-ID<mailman.2470.1350587935.27098.python-list@python.org>
Terry Reedy wrote:
> On 10/18/2012 1:23 PM, Demian Brecht wrote:
> 
> > 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?
> 
> See below.
> 
> > 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
> 
> Or the length could be the difference of two pointers -- address of the
> first empty slot minus address of first item.
> 
> > that correct? If so, is it safe to assume that at least all built-in
> > types observe this behavior,
> 
> str, bytes, bytearrays, arrays, sets, frozensets, dicts, dictviews, and
> ranges should all return len in O(1) time. That includes the possibility
> of a subtraction as indicated above.
> 

Why does pointer arithmetic work for dicts? I would think the position
of a value would be based on the hash of the key and thus "random" for
the context of this conversation.

Ramit Prasad


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web