Most viewed recipes tagged "oop"http://code.activestate.com/recipes/tags/oop/views/2014-09-29T07:15:33-07:00ActiveState Code RecipesConstants 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> 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> The Singleton Pattern implemented with Python (Python) 2001-04-05T21:21:30-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/52558-the-singleton-pattern-implemented-with-python/ <p style="color: grey"> Python recipe 52558 by <a href="/recipes/users/98061/">Jürgen Hermann</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>The following class shows how to implement the singleton pattern[1] in Python. A singleton is a class that makes sure only one instance of it is ever created. Typically such classes are used to manage resources that by their very nature can only exist once.</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> Infix 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> 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> Factory pattern (Python) 2001-11-06T10:54:30-08:00Andres Tuellshttp://code.activestate.com/recipes/users/139364/http://code.activestate.com/recipes/86900-factory-pattern/ <p style="color: grey"> Python recipe 86900 by <a href="/recipes/users/139364/">Andres Tuells</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>In the factory pattern you have an object that creates other objects.</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> Create objects from variable class names (Python) 2004-06-23T12:40:01-07:00Peter van Kampenhttp://code.activestate.com/recipes/users/694894/http://code.activestate.com/recipes/285262-create-objects-from-variable-class-names/ <p style="color: grey"> Python recipe 285262 by <a href="/recipes/users/694894/">Peter van Kampen</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 3. </p> <p>I sometimes want to create objects from various classes based on some condition (without using eval()). For example when parsing X(HT)ML files I want to handle some tags using specific classes. It is actually quite easy but I had a hard time finding out how to do that, no doubt due to my limited knowledge of python, so here's a simple example.</p> <p>Edit: See also &lt;a href="http://www.faqts.com/knowledge_base/view.phtml/aid/2635/fid/242"&gt;http://www.faqts.com/knowledge_base/view.phtml/aid/2635/fid/242&lt;/a&gt;</p> Overriding __new__ for attribute initialization (Python) 2004-09-01T22:42:50-07:00Dan Perlhttp://code.activestate.com/recipes/users/1634351/http://code.activestate.com/recipes/303059-overriding-__new__-for-attribute-initialization/ <p style="color: grey"> Python recipe 303059 by <a href="/recipes/users/1634351/">Dan Perl</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>Whenever a superclass implements a __init__ method to initialize its attributes, subclasses derived from it have to invoke the __init__ method of the superclass. This recipe is a different mechanism of initializing the attributes of a superclass with default values, achieved by overriding the __new__ method.</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> Abstract methods/classes (Python) 2004-01-23T23:16:14-08:00Ivo Timmermanshttp://code.activestate.com/recipes/users/1590094/http://code.activestate.com/recipes/266468-abstract-methodsclasses/ <p style="color: grey"> Python recipe 266468 by <a href="/recipes/users/1590094/">Ivo Timmermans</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>The point is that python doesn't have a notion of “abstract methods.” Abstract methods are part of an base class that defines an interface, without any code. Abstract methods can't be called directly, because they don't contain any code in their definition.</p> <p>In the definition of the base class, you may want to include a specific method that is part of the interface, but the specific implementation is still unknown. A popular example seems to be the drawing of a point or a line in a graphical application.</p> <p>The classes Point and Line share several implementation details, but differ on other. In particular, the way they are drawn is completely different (you will want to optimize the drawing of a line). Suppose these two classes are derived from the same class, Object. It is possible to separate the implementation of the method draw of these two classes, while draw can still be called from the base class Object.</p> Dynamically added methods to a class (Python) 2001-10-15T02:49:01-07:00Brett Cannonhttp://code.activestate.com/recipes/users/98030/http://code.activestate.com/recipes/81732-dynamically-added-methods-to-a-class/ <p style="color: grey"> Python recipe 81732 by <a href="/recipes/users/98030/">Brett Cannon</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>Ruby has the functionality of being able to add a method to a class at an arbitrary point in your code. I figured Python must have some way for allowing this to happen, and it turned out it did. The method is available instantly to all already existing instances and of course ones yet to be created. If you specify method_name then that name is used for the method call.</p> <p>One thing to make sure to do is that the function has a variable for the instance to be passed to (i.e. self).</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> 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> 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> Constructor and Destructor Chaining (Python) 2002-08-24T06:19:55-07:00Michael Soulierhttp://code.activestate.com/recipes/users/645856/http://code.activestate.com/recipes/146462-constructor-and-destructor-chaining/ <p style="color: grey"> Python recipe 146462 by <a href="/recipes/users/645856/">Michael Soulier</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>Chaining constructor and destructor calls together during object creation/destruction.</p> Automatically initializing instance variables from __init__ parameters (Python) 2005-09-21T19:10:00-07:00Henry Crutcherhttp://code.activestate.com/recipes/users/1319534/http://code.activestate.com/recipes/286185-automatically-initializing-instance-variables-from/ <p style="color: grey"> Python recipe 286185 by <a href="/recipes/users/1319534/">Henry Crutcher</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 3. </p> <p>This recipe assigns each parameter to an instance variable of the same name, automating a common pattern of object initialization, and making class definitions more compact.</p> __getattr__: handle with care (Python) 2001-03-12T10:05:37-08:00Jose Sebrosahttp://code.activestate.com/recipes/users/98132/http://code.activestate.com/recipes/52238-__getattr__-handle-with-care/ <p style="color: grey"> Python recipe 52238 by <a href="/recipes/users/98132/">Jose Sebrosa</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>__getattr__ is a very nice and powerful method that must be handled with care, otherwise you can discover that it has more power than you expected...</p>