July 28, 2018
These are materials to accompany my PyOhio talk on improving ones python skills through having fun with the double underscore (dunder) special python methods.
The misfit class:
import random
class CarKey:
"""
Gets lost easily
"""
def __hash__(self):
return random.randint(1, 2)
Some ideas:
CarKey
objects.
CarKey
objects
CarKey
to the set multiple times.CarKey
hash to return either 1 or 9.
CarKey
to a dictionary. Then
observe the change in behavior when more keys are added.The correctly adding version:
def square(x):
return x*x
def yolo(s):
return s + 'YOLO'
class ChurchInt(int):
"""
A Church Number
"""
def __call__(self, fun):
def new_fun(arg):
for idx in range(self):
arg = fun(arg)
return arg
return new_fun
def __add__(self, other):
return ChurchInt(super().__add__(other))
Addition at work:
>>> three = ChurchInt(3)
>>> four = ChurchInt(4)
>>> seven = three + four
>>> y7 = seven(yolo)
>>> y7('spam')
'spamYOLOYOLOYOLOYOLOYOLOYOLOYOLO'
square(three)(square)(2)
!“Resurrecting” version of the object:
limbo = []
class NotDeadYet:
"""
Not quite dead
"""
def __del__(self):
print("I'm not dead yet!")
limbo.append(self)
Add an init method with a self reference:
def __init__(self):
self.ref = self
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.