Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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.

PyCon 2014

April 11, 2014
Tweet

More Decks by PyCon 2014

Other Decks in Programming

Transcript

  1. Import-ant Decisions
    Allison Kaptur!
    PyCon 2014!
    [email protected]!
    github.com/akaptur!
    @akaptur

    View Slide

  2. THIS IS A
    COUP

    View Slide

  3. import

    View Slide

  4. The problem

    View Slide

  5. Copy & paste

    View Slide

  6. A magical copy & paste

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  10. Call a function

    View Slide

  11. exec

    View Slide

  12. Call a function

    View Slide

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

    View Slide

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

    View Slide

  15. View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  19. Solution: namespaces
    France Canada

    View Slide

  20. Module

    View Slide

  21. Module

    View Slide

  22. exec with a namespace

    View Slide

  23. exec with a namespace

    View Slide

  24. exec with a namespace

    View Slide

  25. Adding a namespace

    View Slide

  26. Adding a namespace
    (This is valid code)

    View Slide

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

    View Slide

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

    View Slide

  29. Executes multiple times

    View Slide

  30. Memoize

    View Slide

  31. Memoize

    View Slide

  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

    View Slide

  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

    View Slide

  34. Options?

    View Slide

  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

    View Slide

  36. Keywords:
    syntactic sugar

    View Slide

  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

    View Slide

  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

    View Slide

  39. (Some) things we haven’t done
    1. Created real modules
    2. Created packages
    3. Worried about loaders
    4. Worried about error handling

    View Slide

  40. Other solutions

    View Slide

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

    View Slide

  42. Ruby modules

    View Slide

  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)

    View Slide

  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

    View Slide

  45. Invisible blind spots of
    Python programmers when
    it comes to ‘import’
    Allison Kaptur!
    PyCon 2014!
    github.com/akaptur!
    @akaptur

    View Slide

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

    View Slide