1 Micropython: A minimal implementation of Python for constrained devices
2
3 * Python provides a rich programming environment
4 * CPython enforces the "official" language version
5 * CPython, Jython, etc. expose the full strength language
6
7 Motivations
8
9 * Run Python programs in "small" devices
10 * Small programs plus few executed instructions
11 * Avoid expensive library code
12 (small footprint, lots of executed instructions)
13
14 Python's flexibility comes at a cost
15
16 * Modules, classes, instances are all objects
17 * Freedom to modify most objects at any time
18 * Difficult to predict eventual behaviour before execution
19 * Difficult to generate optimisations when compiling
20
21 Not all things are dynamic/equal in Python
22
23 * Locals, modules and classes are special in some way
24 * Locals don't tend to change unilaterally
25 * Modules usually retain their identity
26 * Classes differ from objects (despite metaclasses)
27
28 Attribute access
29
30 * Must do a full lookup every time:
31 * Check instance dictionary
32 * Check class, superclasses
33 * Potentially lots of code
34 * Lots of executed instructions
35
36 Improving attribute access performance
37
38 * Achieve faster access using a different representation
39 * Must define attributes of objects in advance
40 * Restriction: modules, classes, instances are "closed"
41 * Evaluate the limitations: are they too disruptive?
42
43 Consequences of revised attribute access
44
45 * Cannot extend the range of attributes on objects of existing classes
46 * Further optimisations:
47 * Restriction: attempt to control modification of attributes
48 * Result: further optimisation of accesses
49
50 Invocations
51
52 * Target checking
53 * Number of arguments vs. number of parameters
54 * Keyword parameter resolution
55 * Defaults
56
57 Other costly operations
58
59 * Binary operators
60 * Comparisons
61
62 Examples of rarely used/unnecessary flexibility
63
64 * Can subclass dynamically:
65
66 for cls in A, B:
67 class C(cls):
68 pass
69
70 * Yet most people would relate to "class C"
71 * Not "the class currently known as C"
72
73 Examples of occasionally used flexibility
74
75 * Can evaluate classes and functions in dynamic contexts:
76
77 if platform == 'posix':
78 class C:
79 ...
80 else:
81 class C:
82 ...
83
84 * Seen quite often in the standard library with functions
85 * Dynamism used for configuration purposes
86
87 Distinguish between "good" and "bad" dynamism
88
89 * Dynamic class preparation
90 * Run-time choice of classes
91 * Assigning attributes to modules
92
93 Run-time configuration
94
95 * The source of a lot of "bad" dynamism
96 * Comparable to, but more robust than...
97 * Class loading tricks in Java
98 * Dynamic library loading magic in C/C++
99 * Has a place, but perhaps not in compiled, embedded programs
100
101 Micropython modules
102
103 * Modules contain attributes as with normal Python
104 * Inside the module:
105 * Attributes can be accessed and set as globals
106 * Classes and functions define module attributes
107 * Outside the module:
108 * Attributes can be accessed but not set
109 * Definition from within means more predictable content
110
111 Micropython classes
112
113 * Classes contain attributes and expose superclass attributes
114 * Inside the class:
115 * Attributes can be accessed and set in the class scope
116 * Functions define methods
117 * Outside the class:
118 * Attributes can be accessed but not set
119 * Definition from within means more predictable content
120
121 Micropython instances
122
123 * Instances contain attributes and expose class attributes
124 * Instance attributes must not shadow class attributes
125 * The set of attributes is detected by scanning the __init__ method
126
127 Rationale for restrictions
128
129 * Construct efficient run-time representations
130 * Predictable content means that access can be optimised
131 * No shadowing means that only a single lookup is necessary
132
133 References, attributes and values
134
135 * Almost everything can be considered as a kind of attribute:
136 * Module, class, instance
137 * Local variable is the exception
138 * Acquired attributes are "values":
139 * An object being manipulated
140 * Its context
141 * Most kinds of values have no real context:
142 * Module and class attributes, locals
143 * The exception:
144 * Instance attributes