Top-rated recipes tagged "abc" but not "collections", "abstract_base_class" and "hierarchy"http://code.activestate.com/recipes/tags/abc-collections-abstract_base_class-hierarchy/top/2011-08-13T03:49:26-07:00ActiveState Code RecipesAdding __implements__ to subclasses during ABCMeta.register (Python)
2011-08-13T03:49:26-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577712-adding-__implements__-to-subclasses-during-abcmeta/
<p style="color: grey">
Python
recipe 577712
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/abc/">abc</a>).
</p>
<p>This is an extension to the abc.ABCMeta class. It is related to <a href="http://code.activestate.com/recipes/577711/">recipe 577711</a>.</p>
<p>Basically it has ABCMeta.register add __implements__ to any subclass that gets registered.</p>
Validating classes and objects against an Abstract Base Class (Python)
2011-05-21T19:14:19-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577711-validating-classes-and-objects-against-an-abstract/
<p style="color: grey">
Python
recipe 577711
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/abc/">abc</a>, <a href="/recipes/tags/validation/">validation</a>).
Revision 4.
</p>
<p>Abstract Bases Classes in Python provide great features for describing interfaces programmatically. By default a subclass is validated against all its ABC parents at instantiation time (in object.__new__). This recipe aims to provide for validation against an ABC of:</p>
<ul>
<li>any class at definition time (including subclasses and registered classes),</li>
<li>any object at any time.</li>
</ul>
<p>I have included an example of the reason I did all this. It allows you to implement an ABC in the instance rather than the class.</p>
<p>If the classes argument to validate is None then it tries to build the list of classes from the object's MRO. If the ABCMeta.register method facilitated an __implements__ list on classes, we could also use that to validate against the registered "base" classes.</p>
<p>The code I have provided is for Python 3, but it should work in 2.7 with a little modification.</p>
<p>This code borrows from Lib/abc.py and objects/typeobject.c</p>