Popular recipes tagged "method"http://code.activestate.com/recipes/tags/method/2016-09-01T12:34:17-07:00ActiveState Code RecipesMethod chaining or cascading (Python) 2016-09-01T12:34:17-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/578770-method-chaining-or-cascading/ <p style="color: grey"> Python recipe 578770 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/cascade/">cascade</a>, <a href="/recipes/tags/cascading/">cascading</a>, <a href="/recipes/tags/chaining/">chaining</a>, <a href="/recipes/tags/method/">method</a>). </p> <p>A frequently missed feature of built-ins like lists and dicts is the ability to chain method calls like this:</p> <pre class="prettyprint"><code>x = [] x.append(1).append(2).append(3).reverse().append(4) # x now equals [3, 2, 1, 4] </code></pre> <p>Unfortunately this doesn't work, as mutator methods return <code>None</code> rather than <code>self</code>. One possibility is to design your class from the beginning with method chaining in mind, but what do you do with those like the built-ins which aren't?</p> <p>This is sometimes called <a href="https://en.wikipedia.org/wiki/Method_cascading">method cascading</a>. Here's a proof-of-concept for an adapter class which turns any object into one with methods that can be chained.</p> Decorator to check method param types (Python) 2014-01-14T11:23:40-08:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/578809-decorator-to-check-method-param-types/ <p style="color: grey"> Python recipe 578809 by <a href="/recipes/users/4176176/">Andrey Nikishaev</a> (<a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/typecheck/">typecheck</a>). </p> <p>This solution give possibility to check method param type, raise needed exception type, and also have good readability in the decorator definition.</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> CLOS-like around/before/after auxiliary methods (Python) 2011-08-25T22:59:22-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577859-clos-like-aroundbeforeafter-auxiliary-methods/ <p style="color: grey"> Python recipe 577859 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/auxiliary/">auxiliary</a>, <a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/clos/">clos</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/object/">object</a>, <a href="/recipes/tags/super/">super</a>). </p> <p>This module provides an easy way to define and use your own <strong>around/before/after auxiliary methods</strong>, similar to <a href="http://www.aiai.ed.ac.uk/~jeff/clos-guide.html#meth-comb">those used in CLOS</a> (Common Lisp Object System).</p> Abstract method decorator (Python) 2011-04-20T21:50:23-07:00jimmy2timeshttp://code.activestate.com/recipes/users/4177690/http://code.activestate.com/recipes/577666-abstract-method-decorator/ <p style="color: grey"> Python recipe 577666 by <a href="/recipes/users/4177690/">jimmy2times</a> (<a href="/recipes/tags/abstract/">abstract</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/subclass/">subclass</a>). Revision 5. </p> <p>A simple decorator that helps define abstract methods: when such a method is called, an appropriate exception is raised.</p> dualmethod descriptor (Python) 2010-02-06T20:54:45-08:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577030-dualmethod-descriptor/ <p style="color: grey"> Python recipe 577030 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/method/">method</a>). Revision 3. </p> <p>This descriptor can be used to decorate methods, similar to the built-ins classmethod and staticmethod. It enables the caller to call methods on either the class or an instance, and the first argument passed to the method will be the class or the instance respectively.</p> <p>This differs from classmethods, which always passes the class, and staticmethods, which don't pass either.</p> <p>Like all descriptors, you can only use this in new-style classes.</p> Method signature type checking decorator for Python 3.0 (Python) 2010-07-10T00:40:54-07:00andrew johnsonhttp://code.activestate.com/recipes/users/4174071/http://code.activestate.com/recipes/577299-method-signature-type-checking-decorator-for-pytho/ <p style="color: grey"> Python recipe 577299 by <a href="/recipes/users/4174071/">andrew johnson</a> (<a href="/recipes/tags/checking/">checking</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/signature/">signature</a>, <a href="/recipes/tags/type/">type</a>). </p> <p>Clean lightweight type-checking with Python 3.0 annotations.</p> Poor man's mgrid (Python) 2009-01-21T15:37:13-08:00David Lamberthttp://code.activestate.com/recipes/users/4167420/http://code.activestate.com/recipes/576625-poor-mans-mgrid/ <p style="color: grey"> Python recipe 576625 by <a href="/recipes/users/4167420/">David Lambert</a> (<a href="/recipes/tags/index/">index</a>, <a href="/recipes/tags/index_tricks/">index_tricks</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/mgrid/">mgrid</a>, <a href="/recipes/tags/numerical/">numerical</a>, <a href="/recipes/tags/numerical_methods/">numerical_methods</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/scipy/">scipy</a>, <a href="/recipes/tags/trick/">trick</a>). </p> <p>Python 3 code. scipy.mgrid is a useful! This short implementation comes without installing numpy.</p> Method signature type checking decorator for Python 3 (Python) 2015-05-15T09:25:08-07:00Dmitry Dvoinikovhttp://code.activestate.com/recipes/users/2475216/http://code.activestate.com/recipes/572161-method-signature-type-checking-decorator-for-pytho/ <p style="color: grey"> Python recipe 572161 by <a href="/recipes/users/2475216/">Dmitry Dvoinikov</a> (<a href="/recipes/tags/checking/">checking</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/interface/">interface</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/signature/">signature</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>, <a href="/recipes/tags/type/">type</a>). Revision 8. </p> <p>This recipe allows nice and clean validation for method parameters/return values. It uses function annotations available in Python 3 for the actual signature specification.</p>