Re: len() on mutables vs. immutables

Discussion in 'Python' started by Demian Brecht, Feb 8, 2013.

  1. 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" <> 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
    Demian Brecht, Feb 8, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Demian Brecht

    len() on mutables vs. immutables

    Demian Brecht, Oct 18, 2012, in forum: Python
    Replies:
    0
    Views:
    100
    Demian Brecht
    Oct 18, 2012
  2. Terry Reedy

    Re: len() on mutables vs. immutables

    Terry Reedy, Oct 18, 2012, in forum: Python
    Replies:
    0
    Views:
    112
    Terry Reedy
    Oct 18, 2012
  3. Demian Brecht

    Re: len() on mutables vs. immutables

    Demian Brecht, Oct 18, 2012, in forum: Python
    Replies:
    0
    Views:
    126
    Demian Brecht
    Oct 18, 2012
  4. Demian Brecht

    Re: len() on mutables vs. immutables

    Demian Brecht, Oct 18, 2012, in forum: Python
    Replies:
    0
    Views:
    116
    Demian Brecht
    Oct 18, 2012
  5. Nick Cash

    RE: len() on mutables vs. immutables

    Nick Cash, Oct 18, 2012, in forum: Python
    Replies:
    0
    Views:
    92
    Nick Cash
    Oct 18, 2012
Loading...

Share This Page