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


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

Re: len() on mutables vs. immutables

Started byDaniel Urban <urban.dani@gmail.com>
First post2012-10-18 21:43 +0200
Last post2012-10-18 21:43 +0200
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 Daniel Urban <urban.dani@gmail.com> - 2012-10-18 21:43 +0200

#31682 — Re: len() on mutables vs. immutables

FromDaniel Urban <urban.dani@gmail.com>
Date2012-10-18 21:43 +0200
SubjectRe: len() on mutables vs. immutables
Message-ID<mailman.2472.1350589418.27098.python-list@python.org>
On Thu, Oct 18, 2012 at 8:42 PM, Demian Brecht <demianbrecht@gmail.com> wrote:
>> 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.
>
> 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), so if you know whereabouts to look, it would be most helpful :)

The source is usually in Objects/*object.c (e.g., the source for list
is in Objects/listobject.c, dict is in dictobject.c and so on). The
implementation of __len__ is usually in a method called
whatever_length (e.g., dict.__len__ is called dict_length). To be
sure, you can check the PyTypeObject declaration for the type.
Probably the tp_as_sequence or tp_as_mapping field contains the
pointer to __len__ (sq_length or mp_length respectively). (You can
also search for "lenfunc", which is the type of such functions.)

[toc] | [standalone]


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


csiph-web