~/talk

Function dissection lab -- learn how functions

Talkat PyCon US 2020 · 2020-04-15..04-23· [python]
slides:none on file

Asserts that Python treats functions as objects and demonstrates the mechanics: inspect __code__ and its fields (co_argcount, co_varnames, co_flags, co_consts), __defaults__, and closure slots; uses the dis module and Python Tutor to use dis to inspect bytecode and to trace frames; shows the surprising mutable-default bug where a list in __defaults__ mutable defaults persist across calls; reproduces scoping behaviors (unbound local, global, nonlocal) with live demos that all execute as described.

AI-written summary · about

  1. 0:00Intro
  2. 0:22I teach Python
  3. 1:06Consider this code
  4. 1:50Function objects? . When you use def. you create a function object.
  5. 3:34Example
  6. 6:51Error messages use this info
  7. 7:25Additional variables
  8. 8:52So, how does Python know?
  9. 10:43Same goes for **kwargs
  10. 11:08dis.show_code
  11. 12:10Constants?
  12. 14:23When Python calls a function...
  13. 15:04Consider this function
  14. 15:47Let's add a default
  15. 17:18Keyword-only arguments
  16. 17:57Where does Python put that?
  17. 18:41Let's talk about scoping
  18. 20:03How does Python know x isn't local?
  19. 20:24Let's make things more complex
  20. 22:50A more common version of this problem
  21. 23:29The "global" declaration
  22. 24:14Bytecodes with a local"X"
  23. 24:41Putting the "E" in LEGB
  24. 27:14What have we learned?
  25. 27:48Questions or comments?
~/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.