Most viewed recipes tagged "classes"http://code.activestate.com/recipes/tags/classes/views/2017-04-20T23:34:50-07:00ActiveState Code RecipesImplementing class-based callbacks in Python (Python)
2017-04-20T23:34:50-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580788-implementing-class-based-callbacks-in-python/
<p style="color: grey">
Python
recipe 580788
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/callbacks/">callbacks</a>, <a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/objects/">objects</a>, <a href="/recipes/tags/programming/">programming</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>This is a follow-on to this recently posted recipe:</p>
<p>Implementing function-based callbacks in Python:
<a href="https://code.activestate.com/recipes/580787-implementing-function-based-callbacks-in-python/?in=user-4173351" rel="nofollow">https://code.activestate.com/recipes/580787-implementing-function-based-callbacks-in-python/?in=user-4173351</a></p>
<p>This new recipe shows how to create and use callbacks in Python, using classes with methods, instead of plain functions, as was done in the recipe linked above. All other points such as reasons and benefits for using callbacks, are more or less the same as mentioned in the previous recipe, except that class instances can carry state around, so to that extent, the two approaches are different.</p>
<p><a href="https://jugad2.blogspot.in/2017/04/python-callbacks-using-classes-and.html" rel="nofollow">https://jugad2.blogspot.in/2017/04/python-callbacks-using-classes-and.html</a></p>
Trace decorator for debugging (Python)
2011-01-24T18:40:51-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577551-trace-decorator-for-debugging/
<p style="color: grey">
Python
recipe 577551
by <a href="/recipes/users/4173535/">Kevin L. Sitze</a>
(<a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/debug/">debug</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/inspection/">inspection</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/trace/">trace</a>).
Revision 2.
</p>
<p>This package provides a decorator for tracing function and method calls in your applications. The tracing capabilities are managed through the logging package, and several mechanisms are provided for controlling the destination of the trace output.</p>
<p>It also provides functionality for adding decorators to existing classes or modules.</p>
Calculate the MRO of a class (Python)
2011-06-11T08:31:09-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577748-calculate-the-mro-of-a-class/
<p style="color: grey">
Python
recipe 577748
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/c3/">c3</a>, <a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/mro/">mro</a>, <a href="/recipes/tags/order/">order</a>, <a href="/recipes/tags/resolution/">resolution</a>).
</p>
<p>This function allows you to calculate the Method Resolution Order (MRO, or sometimes linearization) of a class or base classes. This is the so-called "C3" algorithm, as used by Python (new-style classes, from version 2.3 and higher). The MRO is the order of base classes that Python uses to search for methods and attributes. For single inheritance, the MRO is obvious and straight-forward and not very exciting, but for multiple inheritance it's not always obvious what the MRO should be.</p>
Mixin and Overlay (Python)
2011-06-01T05:16:47-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577730-mixin-and-overlay/
<p style="color: grey">
Python
recipe 577730
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/mixins/">mixins</a>).
</p>
<h4 id="the-two-sides-of-classes">The Two Sides of Classes</h4>
<p>Class inheritance either adds new attributes to a class, or changes existing attributes. However, this distinction is not made in Python's class system. This recipe is a stab at providing that separation. It takes advantage of metaclasses and class decorators to do it.</p>
<p>For this recipe the two sides of class inheritance are called mixins and overlays. Mixins add new attributes to a class. Overlays change existing attributes, which in the case of methods means changing the behavior of the methods. However, overlays do not add any attributes.</p>
<h4 id="separation-of-concerns">Separation of Concerns</h4>
<p>Another issue with class inheritance is that abstraction rarely breaks down into perfect trees with a clear separation of concerns. Python allows multiple inheritance, which can help, but requires cooperation between classes in the diamond hierarchy. Mixins and overlays help with this problem. Hopefully, that will be evidenced by the recipe and subsequent examples.</p>
<p>With this approach, the main single inheritance line can focus on the core abstraction and mixins/overlays can be used to extend the classes in other directions. This does not solve all the problems regarding separation of concerns, but it solves some. I hope to address the rest in another recipe that centers around delegation through a component architecture on instances.</p>
<h4 id="interfaces">Interfaces</h4>
<p>Since Python 2.6 we have had Abstract Base Classes providing a mechanism for promising what interfaces an object provides. I'll show in one of the examples how an ABC can be split into a interface portion and a mixin portion. The recipe takes advantage of the ABC functionality in the Python type system. I expect that mixins and overlays would be a good fit with one of my other recipes (<a href="http://code.activestate.com/recipes/577711/">Recipe 577711</a>).</p>
<h4 id="the-recipe-classes">The Recipe Classes</h4>
<p>This recipe provides a standard approach to applying mixin classes without using inheritance. This is done through a metaclass. The metaclass builds a __mixins__ attribute on the class and provides a mixes_in class decorator. Applying that decorator to the class will add the attributes in __mixins__ to the decorated class. However, if any of those names are already bound on the name then it will fail. The decorator returns the modified class.</p>
<p><strong>Note</strong>: Traditional mixins are typically done through multiple inheritance, as opposed to class decorators.</p>
<p>This recipe also provides a companion to mixin classes, called overlays. This is done through a metaclass in exactly the same way as the mixins, but provides an __overlays__ attribute and a class decorator called overlays. In contrast to mixins, if an overlay attribute is missing on the decorated class, it will fail. This is because the decorator will return a new class that inherits from the decorated class, and overrides the attributes in __overlays__.</p>
<p><strong>Note</strong>: Metaclasses are used here because we need to pull from the class namespace there. A class decorator does not afford us the same functionality without more complexity.</p>
<p><strong>Note</strong>: This recipe should work fine in 2.7 with a switch to the __metaclass__ syntax.</p>
Money Game (Python)
2014-12-22T23:38:38-08:00Lance Spencehttp://code.activestate.com/recipes/users/4191386/http://code.activestate.com/recipes/578988-money-game/
<p style="color: grey">
Python
recipe 578988
by <a href="/recipes/users/4191386/">Lance Spence</a>
(<a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/games/">games</a>).
</p>
<p>I created this simple program as a possible start to something larger while I continue to learn Python. It's a simple program that can be used to help young kids learn to count change in US currency.</p>
Simple Abstract "Constants" to Use When @abstractproperty is Overkill or Misleading (Python)
2011-08-12T23:35:45-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577761-simple-abstract-constants-to-use-when-abstractprop/
<p style="color: grey">
Python
recipe 577761
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/classes/">classes</a>).
</p>
<p>Use these instead of abstract properties when you don't plan on the abstract attribute being implemented with a property. And you can still give your attribute a docstring!</p>
Make a Class's Name Available in its Definition Body (Python)
2011-08-04T21:38:40-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577813-make-a-classs-name-available-in-its-definition-bod/
<p style="color: grey">
Python
recipe 577813
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/classes/">classes</a>).
</p>
<p>Since a class object is created <em>after</em> the body is executed, it can't be available to the class body. Even the name is unavailable, at least by default. However, you can use the <code>__prepare__()</code> method in a metaclass to stick it in there. This recipe is a simple demonstration of how.</p>
Turn a Function Into a Class (Python)
2011-10-05T18:38:43-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577822-turn-a-function-into-a-class/
<p style="color: grey">
Python
recipe 577822
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/functions/">functions</a>).
</p>
<p>The only catch is that the function has to return locals() at the end. And it doesn't do the __prepare__ part of 3.x metaclasses.</p>
Tracking Child Classes (Python)
2012-05-30T10:00:21-07:00Evert van de Waalhttp://code.activestate.com/recipes/users/4182118/http://code.activestate.com/recipes/578137-tracking-child-classes/
<p style="color: grey">
Python
recipe 578137
by <a href="/recipes/users/4182118/">Evert van de Waal</a>
(<a href="/recipes/tags/child/">child</a>, <a href="/recipes/tags/classes/">classes</a>).
</p>
<p>This recipe allows a base class to keep track of the child classes that inherit from it using a metaclass.</p>