~/talk

How does Python get the length with len() function?

Talkat PyCon Philippines 2024 · 2024-02-25..02-26· [python]
slides:none on file

Shows that len() calls __len__ and enforces an int return, raising TypeError if __len__ returns non-int and OverflowError for absurdly large values. Demonstrates with lists, dicts and custom classes (including one that returns changing or non-int lengths) and confirms behavior by reading the CPython source and design/history FAQ and PEPs. Frames len as an adapter pattern that validates and converts object-provided length semantics, and shows the bool logic uses __bool__ or falls back to __len__ with the same checks.

AI-written summary · about

  1. 0:00Introduction
  2. 2:55How len() works
  3. 6:36Adapter pattern and len()
  4. 11:21Protocols and len()
  5. 15:58bool() adapter
  6. 21:04Summary
  7. 25:07Q&A
~/search
type to search 6,249 conferences, 138,987 speakers and 185,172 talks
~/help
cmdctrl k
search every conference, speaker and series
/
focus the filter search
h
home
c
conferences
s
speakers
e
series
t
light / dark — the status bar button does the same
?
this help
esc
close this, then clear every filter — or just the search, while you are typing in it

Ignored while you are typing. Pages without a filter rail have no search to focus.