Top-rated recipes tagged "oop"http://code.activestate.com/recipes/tags/oop/top/2014-09-29T07:15:33-07:00ActiveState Code RecipesInfix operators (Python) 2005-02-17T11:12:04-08:00Ferdinand Jamitzkyhttp://code.activestate.com/recipes/users/98863/http://code.activestate.com/recipes/384122-infix-operators/ <p style="color: grey"> Python recipe 384122 by <a href="/recipes/users/98863/">Ferdinand Jamitzky</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 3. </p> <p>Python has the wonderful "in" operator and it would be nice to have additional infix operator like this. This recipe shows how (almost) arbitrary infix operators can be defined.</p> Singleton? We don't need no stinkin' singleton: the Borg design pattern (Python) 2001-08-27T08:05:21-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/66531-singleton-we-dont-need-no-stinkin-singleton-the-bo/ <p style="color: grey"> Python recipe 66531 by <a href="/recipes/users/97991/">Alex Martelli</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>The Singleton design pattern (DP) has a catchy name, but the wrong focus -- on identity rather than on state. The Borg design pattern has all instances share state instead, and Python makes it, literally, a snap.</p> Dependency Injection The Python Way (Python) 2007-03-10T11:26:16-08:00Zoran Isailovskihttp://code.activestate.com/recipes/users/2400454/http://code.activestate.com/recipes/413268-dependency-injection-the-python-way/ <p style="color: grey"> Python recipe 413268 by <a href="/recipes/users/2400454/">Zoran Isailovski</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 9. </p> <p>Sample Pythonic Inversion-of-Control Pseudo-Container.</p> The simple but handy "collector of a bunch of named stuff" class (Python) 2001-04-08T19:08:56-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a-bunch-of-named/ <p style="color: grey"> Python recipe 52308 by <a href="/recipes/users/97991/">Alex Martelli</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>Often we want to just collect a bunch of stuff together, naming each item of the bunch; a dictionary's OK for that, but a small do-nothing class is even handier, and prettier to use.</p> A tidy property idiom (Python) 2003-08-01T14:57:43-07:00Sean Rosshttp://code.activestate.com/recipes/users/761068/http://code.activestate.com/recipes/205183-a-tidy-property-idiom/ <p style="color: grey"> Python recipe 205183 by <a href="/recipes/users/761068/">Sean Ross</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 5. </p> <p>This recipe suggests an idiom for property creation that avoids cluttering the class space with get/set/del methods that will not be used directly.</p> SOLVING THE METACLASS CONFLICT (Python) 2014-09-29T07:15:33-07:00Michele Simionatohttp://code.activestate.com/recipes/users/1122360/http://code.activestate.com/recipes/204197-solving-the-metaclass-conflict/ <p style="color: grey"> Python recipe 204197 by <a href="/recipes/users/1122360/">Michele Simionato</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 5. </p> <p>Any serious user of metaclasses has been bitten at least once by the infamous metaclass/metatype conflict. Here I give a general recipe to solve the problem, as well as some theory and some examples.</p> Automatic attribute assignment (Python) 2008-03-11T14:25:40-07:00Arnaud Delobellehttp://code.activestate.com/recipes/users/4059385/http://code.activestate.com/recipes/551763-automatic-attribute-assignment/ <p style="color: grey"> Python recipe 551763 by <a href="/recipes/users/4059385/">Arnaud Delobelle</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>This recipe defines a decorator @autoassign that makes methods assign some or all of their arguments automatically to attributes of self.</p> Rich iterator wrapper (Python) 2006-11-19T00:03:21-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/498272-rich-iterator-wrapper/ <p style="color: grey"> Python recipe 498272 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>This recipe may be of interest to those who make heavy use of the itertools module. It provides a wrapper class that exposes most itertools functions as methods, plus a few more. Moreover, two frequently used itertools functions, chain and islice, are conveniently exposed as addition and slicing operators, respectively.</p> Memento Closure (Python) 2005-05-11T07:44:46-07:00Zoran Isailovskihttp://code.activestate.com/recipes/users/2400454/http://code.activestate.com/recipes/413838-memento-closure/ <p style="color: grey"> Python recipe 413838 by <a href="/recipes/users/2400454/">Zoran Isailovski</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 3. </p> <p>The memento pattern is great for transaction-like processing. Having a handy implementation around might not be the worst thing.</p> C#-Style Events in Python (Python) 2008-10-26T06:35:11-07:00Zoran Isailovskihttp://code.activestate.com/recipes/users/2400454/http://code.activestate.com/recipes/410686-c-style-events-in-python/ <p style="color: grey"> Python recipe 410686 by <a href="/recipes/users/2400454/">Zoran Isailovski</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 7. </p> <p>IMPROVED. The concept of events is heavily used in GUI libraries and is the foundation for most implementations of the MVC (Model, View, Controller) design pattern (the latter being my prime motivation for this recipe). Another prominent use of events is in communication protocol stacks, where lower protocol layers need to inform upper layers of incoming data and the like. Here is a handy class that encapsulates the core to event subscription and event firing and feels like a "natural" part of the language.</p> Chained map lookups (Python) 2004-10-24T09:16:19-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/305268-chained-map-lookups/ <p style="color: grey"> Python recipe 305268 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>Encapsulates lookups into a series of namespaces.</p> Observer Pattern (Python) 2003-01-13T09:38:15-08:00Jørgen Cederberghttp://code.activestate.com/recipes/users/359004/http://code.activestate.com/recipes/131499-observer-pattern/ <p style="color: grey"> Python recipe 131499 by <a href="/recipes/users/359004/">Jørgen Cederberg</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>This is a Python implementation of the observer pattern described by Gamma et. al. It defines a one-to many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.</p> <p>The example should output: Setting Data 1 = 10 DecimalViewer: Subject Data 1 has data 10 HexViewer: Subject Data 1 has data 0xa Setting Data 2 = 15 HexViewer: Subject Data 2 has data 0xf DecimalViewer: Subject Data 2 has data 15 Setting Data 1 = 3 DecimalViewer: Subject Data 1 has data 3 HexViewer: Subject Data 1 has data 0x3 Setting Data 2 = 5 HexViewer: Subject Data 2 has data 0x5 DecimalViewer: Subject Data 2 has data 5 Detach HexViewer from data1 and data2. Setting Data 1 = 10 DecimalViewer: Subject Data 1 has data 10 Setting Data 2 = 15 DecimalViewer: Subject Data 2 has data 15</p> "Static-methods" (aka "class-methods") in Python (Python) 2001-06-19T10:16:00-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52304-static-methods-aka-class-methods-in-python/ <p style="color: grey"> Python recipe 52304 by <a href="/recipes/users/97991/">Alex Martelli</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 3. </p> <p>An attribute of a class-object is implicitly mutated into an unbound-method object if it starts out as a Python-coded function; thus, such functions must be wrapped as other callables if "just calling them" (without an instance-argument) is desired.</p> Observer Pattern (Python) 2013-02-04T20:55:10-08:00Ilya Pashchenkohttp://code.activestate.com/recipes/users/4185152/http://code.activestate.com/recipes/578449-observer-pattern/ <p style="color: grey"> Python recipe 578449 by <a href="/recipes/users/4185152/">Ilya Pashchenko</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>This is a Python implementation of the observer pattern described by Gamma et. al. It defines a one-to many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.</p> <p>The example should output: Setting Data 1 = 10 DecimalViewer: Subject Data 1 has data 10 HexViewer: Subject Data 1 has data 0xa Setting Data 2 = 15 HexViewer: Subject Data 2 has data 0xf DecimalViewer: Subject Data 2 has data 15 Setting Data 1 = 3 DecimalViewer: Subject Data 1 has data 3 HexViewer: Subject Data 1 has data 0x3 Setting Data 2 = 5 HexViewer: Subject Data 2 has data 0x5 DecimalViewer: Subject Data 2 has data 5 Detach HexViewer from data1 and data2. Setting Data 1 = 10 DecimalViewer: Subject Data 1 has data 10 Setting Data 2 = 15 DecimalViewer: Subject Data 2 has data 15</p> Boring Constructor Pattern (Python) 2005-08-08T15:55:36-07:00Ravi Teja Bhupatirajuhttp://code.activestate.com/recipes/users/2545966/http://code.activestate.com/recipes/438819-boring-constructor-pattern/ <p style="color: grey"> Python recipe 438819 by <a href="/recipes/users/2545966/">Ravi Teja Bhupatiraju</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 5. </p> <p>Automate boring constructors that simply create lots of private member variables.</p> Property decorator for python 2.4 (Python) 2005-04-25T23:32:11-07:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/410698-property-decorator-for-python-24/ <p style="color: grey"> Python recipe 410698 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 3. </p> <p>This recipe refines an older recipe on creating class properties ( <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/205183" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/205183</a>). The refinement consists of: - Using a decorator (introduced in python 2.4) to "declare" a function in class scope as property. - Using a trace function to capture the locals() of the decorated function, instead of requiring the latter to return locals(), as in the older recipe.</p> Cache decorator in python 2.4 (Python) 2004-11-01T19:11:21-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/325205-cache-decorator-in-python-24/ <p style="color: grey"> Python recipe 325205 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>The latest version of Python introduced a new language feature, function and method decorators (PEP 318, <a href="http://www.python.org/peps/pep-0318.html" rel="nofollow">http://www.python.org/peps/pep-0318.html</a>). This recipe show a common callable transformation that can benefit from the new syntax, often referred to as Memoization pattern.</p> handler stack (Python) 2004-09-01T18:00:14-07:00Dan Perlhttp://code.activestate.com/recipes/users/1634351/http://code.activestate.com/recipes/302422-handler-stack/ <p style="color: grey"> Python recipe 302422 by <a href="/recipes/users/1634351/">Dan Perl</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 3. </p> <p>Design pattern that is highly reusable. Simple handlers implement one specific task from a complex set of tasks to be performed on an object. Such handlers can then be layered in a stack, in different combinations, together achieving complex processing of an object. New handlers are easy to implement and add.</p> Constants in Python (Python) 2001-08-20T07:12:14-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/65207-constants-in-python/ <p style="color: grey"> Python recipe 65207 by <a href="/recipes/users/97991/">Alex Martelli</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>In Python, any variable can be re-bound at will -- and modules don't let you define special methods such as an instance's __setattr__ to stop attribute re-binding. Easy solution (in Python 2.1 and up): use an instance as "module"...</p> Multicasting on objects (Python) 2001-12-23T19:53:33-08:00Eduard Hitihttp://code.activestate.com/recipes/users/98164/http://code.activestate.com/recipes/52289-multicasting-on-objects/ <p style="color: grey"> Python recipe 52289 by <a href="/recipes/users/98164/">Eduard Hiti</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>Use the 'Multicast' class to multiplex messages/attribute requests to objects which share the same interface.</p>