110 | | === Raw `grep` results === |
111 | | |
112 | | ==== Globals accessed with the `global` keyword ==== |
| 110 | |
| 111 | == Class attributes == |
| 112 | |
| 113 | Class attributes are shared between instances and thus between threads as well |
| 114 | (as module-level classes are just global class objects). |
| 115 | |
| 116 | The behaviour is similar to globals: in similar manner to the global keyword in |
| 117 | functions, explicit class specifier `foo.__class__.bar` is required for setting |
| 118 | class variable `bar` from instance `foo`, otherwise a instance scope variable |
| 119 | will be created that hides the class scope variable. |
| 120 | |
| 121 | (As this may not be obvious, let me illustrate it:) |
| 122 | {{{ |
| 123 | >>> class Foo(object): bar = 1 |
| 124 | ... |
| 125 | >>> f = Foo() |
| 126 | >>> f.bar = 2 |
| 127 | >>> Foo.bar |
| 128 | 1 |
| 129 | >>> f.bar |
| 130 | 2 |
| 131 | >>> f.__class__.bar |
| 132 | 1 |
| 133 | >>> f.__class__.bar = 3 |
| 134 | >>> f.bar |
| 135 | 2 |
| 136 | >>> Foo.bar |
| 137 | 3 |
| 138 | }}} |
| 139 | |
| 140 | As with globals, |
| 141 | 1. class variables that are assigned to when the class is defined and never modified later (THREAD-SAFE), |
| 142 | 1. mutable class level data structures (lists and dictionaries, also instances) that are assigned to when the class is defined but whose elements are modified in methods and that are accessed without using the `__class__` keyword (NOT THREAD-SAFE), |
| 143 | 1. class variables assigned to in methods by using the `__class__` keyword (NOT THREAD-SAFE), |
| 144 | |
| 145 | Metaclasses -- think through the implications. |
| 146 | |
| 147 | == Raw `grep` results == |
| 148 | |
| 149 | === Globals accessed with the `global` keyword === |
250 | | == Class attributes == |
251 | | |
252 | | Class attributes are shared between instances and thus between threads as well |
253 | | (as module-level classes are just global class objects). |
254 | | |
255 | | The behaviour is similar to globals: in similar manner to the global keyword in |
256 | | functions, explicit class specifier `foo.__class__.bar` is required for setting |
257 | | class variable `bar` from instance `foo`, otherwise a instance scope variable |
258 | | will be created that hides the class scope variable. |
259 | | |
260 | | (As this may not be obvious, let me illustrate it:) |
261 | | {{{ |
262 | | >>> class Foo(object): bar = 1 |
263 | | ... |
264 | | >>> f = Foo() |
265 | | >>> f.bar = 2 |
266 | | >>> Foo.bar |
267 | | 1 |
268 | | >>> f.bar |
269 | | 2 |
270 | | >>> f.__class__.bar |
271 | | 1 |
272 | | >>> f.__class__.bar = 3 |
273 | | >>> f.bar |
274 | | 2 |
275 | | >>> Foo.bar |
276 | | 3 |
277 | | }}} |
278 | | |
279 | | As with globals, |
280 | | 1. class variables that are assigned to when the class is defined and never modified later (THREAD-SAFE), |
281 | | 1. mutable class level data structures (lists and dictionaries, also instances) that are assigned to when the class is defined but whose elements are modified in methods and that are accessed without using the `__class__` keyword (NOT THREAD-SAFE), |
282 | | 1. class variables assigned to in methods by using the `__class__` keyword (NOT THREAD-SAFE), |
283 | | |
284 | | Metaclasses -- think through the implications. |
285 | | |
286 | | === Raw `grep` results === |
287 | | |
288 | | ==== `__class__` keyword used for accessing anything other than `__name__` ==== |
| 287 | === `__class__` keyword used for accessing anything other than `__name__` === |