These are materials to accompany my
PyOhio talk
on improving ones python skills through having fun with the
double underscore (dunder) special python methods.
CarKeys
The misfit class:
Some ideas:
Try making a dictionary keyed by CarKey objects.
See what happens when you assign to keys, retrieve values
Look at sets that contain CarKey objects
Add a CarKey to the set multiple times.
Try changing the CarKey hash to return either 1 or 9.
As of CPython 3.7.0, dictionaries start with eight hash
buckets. Try adding the 1-9 CarKey to a dictionary. Then
observe the change in behavior when more keys are added.
For the truly curious, you can see how buckets are selected
from the hash from the CPython
source code comments
(as of this writing)
ChurchInt
The correctly adding version:
Addition at work:
>>> three = ChurchInt(3)
>>> four = ChurchInt(4)
>>> seven = three + four
>>> y7 = seven(yolo)
>>> y7('spam')
'spamYOLOYOLOYOLOYOLOYOLOYOLOYOLO'
Try adding other mathematical operations.
Once you implement multiplication you can even do
things like square(three)(square)(2)!
NotDeadYet
“Resurrecting” version of the object:
Try making the object and deleting all references to it.
Add an init method with a self reference:
Now the object will need to wait on the garbage collector for
destruction due to the circular reference. You can do
import gc; gc.collect() to make the garbage collector run.