Slide 1

Slide 1 text

Python imports, reloads (and associated dangers)

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

A rough reconstruction of a work day I had. (with less swearing)

Slide 4

Slide 4 text

Let’s add a few quick prints and try that again.

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Once more, with id()

Slide 7

Slide 7 text

Conclusions ● importlib.reload will not reload things loaded as ‘from import ○ 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

Slide 8

Slide 8 text

Thanks! saoili@