August 2018
Intermediate to advanced
366 pages
10h 14m
English
Our bunch implementation is not yet complete, as it will fail any test for class name (it's always named Bunch) and any test for inheritance, thus failing at faking other objects.
The first step is to make Bunch able to shapeshift not only its properties, but also its name. This can be achieved by creating a new class dynamically every time we create Bunch. The class will inherit from Bunch and will do nothing apart from providing a new name:
>>> class BunchBase(dict): ... def __getattribute__(self, key): ... try: ... return self[key] ... except KeyError: ... raise AttributeError(key) ... ... def __setattr__(self, key, value): ... self[key] = value ... >>> def Bunch(_classname="Bunch", **attrs): ... return type(_classname, ...