On Sun, Dec 31, 2017 at 08:53:49PM -0600, boB Stepp wrote: > I was wondering if len(a_list) is computed on the fly or is it a > stored attribute of the a_list object? And is the answer the same for > both Python 2 and 3?
Technically the Python language doesn't make any guarantees about this, but in practice it is a stored attribute of the list (as well as other built-ins like tuples, strings, dicts and sets). The answer is the same for both Python 2 and 3, and all the major Python implementations (CPython, IronPython, Jython, PyPy). But technically I guess it counts as a "quality of implementation" marker: crappy Python interpreters might count the items in a list on the fly, good ones will store it as a pre-computed attribute. In practice, it is probably fine to assume that calling len() on built-ins is fast, but for third-party sequences and collections it might not be. -- Steve _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor