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

Python imports, reloads, and associated dangers by Sorcha Bowler

Python imports, reloads, and associated dangers by Sorcha Bowler

This talk is part of "PyLadies Dublin Aug Virtual Event - Poetry | Python Imports"
- When: Tuesday, August 18, 2020
- Event Page: https://www.meetup.com/PyLadiesDublin/events/271722192/

Talk Description:
I lost a half a day's work to trying to find a bug that didn't exist because reloading imports didn't do what I thought it would. I'm here to save you that fate.

About Sorcha Bowler:
Sorcha studied computer science in college, did a non-coding job for many years, taught themself python, got a coding job, and now writes code for Google and is still rather surprised by that.

3476530ee3199731f810cb41daadad79?s=128

PyLadies Dublin

August 18, 2020
Tweet

More Decks by PyLadies Dublin

Other Decks in Technology

Transcript

  1. Python imports, reloads (and associated dangers)

  2. A quick aside about Enums • Enum is short for

    Enumerated Type • I’ve come across them far more in other languages, but they’re in Python >>> from enum import Enum >>> class Color(Enum): ... RED = 1 ... GREEN = 2 ... BLUE = 3 • Now you can use Color.RED as a constant • Color.RED is always Color.RED. Color.GREEN is always Color.GREEN
  3. A rough reconstruction of a work day I had. (with

    less swearing)
  4. Let’s add a few quick prints and try that again.

  5. A quick aside about Identity in python • Python separates

    out the ideas of ‘is the same as’ (= =) and ‘is the same actual thing as’ (is) ◦ Mostly we come across this as being told that we should use ‘is None’ instead of ‘== None’ because something something custom comparison operators • You can get the identity of an object with the id() function • Interestingly, python stores a single copy of the integers from -5 to 256, but creates a new object for integers outside that range each time, so >>> a = 256 >>> b = 256 >>> a is b True >>> a = 257 >>> b = 257 >>> a is b False
  6. Once more, with id()

  7. Conclusions • importlib.reload will not reload things loaded as ‘from

    <x> import <y> ◦ One way around this (I learned, while writing these slides) would have been this: ▪ >>> import file_b ▪ >>> file_b.MyEnum ▪ Instead of this ▪ >>> from file_b import MyEnum ▪ >>> MyEnum • Enum identity is set when the file it is in is imported
  8. Thanks! saoili@<most of the internet>