Save 37% off PRO during our Black Friday Sale! »

Import-ant Decisions by Allison Kaptur

Import-ant Decisions by Allison Kaptur

Suppose `import` didn't exist, and we had to invent it from scratch. We'll look at the problem - code sharing and reuse across modules - and different ways we could solve it. We'll come up with `import` from parallel universes and reinvent python's actual implementation.

D21717ea76044d31115c573d368e6ff4?s=128

PyCon 2014

April 11, 2014
Tweet

Transcript

  1. Import-ant Decisions Allison Kaptur! PyCon 2014! allison.kaptur@gmail.com! github.com/akaptur! @akaptur

  2. THIS IS A COUP

  3. import

  4. The problem

  5. Copy & paste

  6. A magical copy & paste

  7. A magical copy & paste Implemented outside of Python (e.g.

    a bash script): ! 1. Take your .py file 2. Look for “%magical_paste useful” 3. Find the file useful.py 4. Replace the magical_paste line with the contents of useful.py
  8. We have problems 1. Static 2. Name collisions 3. Executes

    multiple times 4. Rigid
  9. We have problems 1. Static 2. Name collisions 3. Executes

    multiple times 4. Rigid
  10. Call a function

  11. exec

  12. Call a function

  13. We have problems 1. Static: use Python at run-time 2.

    Name collisions 3. Executes multiple times 4. Rigid
  14. We have problems 1. Static: use Python at run-time 2.

    Name collisions 3. Executes multiple times 4. Rigid
  15. None
  16. Solution: namespaces Montréal Montréal Images: http://en.wikipedia.org/wiki/Montr%C3%A9al,_Ard%C3%A8che http://en.wikipedia.org/wiki/Montreal

  17. Solution: namespaces Montréal, France Montréal, Canada

  18. Solution: namespaces France.Montréal Canada.Montréal

  19. Solution: namespaces France Canada

  20. Module

  21. Module

  22. exec with a namespace

  23. exec with a namespace

  24. exec with a namespace

  25. Adding a namespace

  26. Adding a namespace (This is valid code)

  27. We have problems 1. Static: use Python at run-time 2.

    Name collisions: use namespaces and modules 3. Executes multiple times 4. Rigid
  28. We have problems 1. Static: use Python at run-time 2.

    Name collisions: use namespaces and modules 3. Executes multiple times 4. Rigid
  29. Executes multiple times

  30. Memoize

  31. Memoize

  32. We have problems 1. Static: use Python at run-time 2.

    Name collisions: use namespaces and modules 3. Executes multiple times: memoize 4. Rigid
  33. We have problems 1. Static: use Python at run-time 2.

    Name collisions: use namespaces and modules 3. Executes multiple times: memoize 4. Rigid
  34. Options?

  35. We have problems 1. Static: use Python at run-time 2.

    Name collisions: use namespaces and modules 3. Executes multiple times: memoize 4. Rigid Ugly
  36. Keywords: syntactic sugar

  37. We have problems 1. Static: use Python at run-time 2.

    Name collisions: use namespaces and modules 3. Executes multiple times: memoize 4. Rigid: use keywords for flexible syntax
  38. check sys.modules to see if name is already imported make

    an empty module (a namespace) find the source code execute the source code in the new, empty module bind name in the caller’s namespace Actual import insert the module into sys.modules
  39. (Some) things we haven’t done 1. Created real modules 2.

    Created packages 3. Worried about loaders 4. Worried about error handling
  40. Other solutions

  41. “magical copy & paste” #include in C

  42. Ruby modules

  43. Modula “A module is a set of procedures, data types,

    and variables, where the programmer has precise control over the names that are imported from and exported to the environment.” ! - N. Wirth, “Modula: A Language for Modular Multiprogramming” (1976)
  44. There are these two young fish swimming along and they

    happen to meet an older fish swimming the other way, who nods at them and says "Morning, boys. How's the water?" And the two young fish swim on for a bit, and then eventually one of them looks over at the other and goes "What the hell is water?” ! - David Foster Wallace, 2005
  45. Invisible blind spots of Python programmers when it comes to

    ‘import’ Allison Kaptur! PyCon 2014! github.com/akaptur! @akaptur
  46. Questions Allison Kaptur! PyCon 2014! github.com/akaptur! @akaptur