Popular recipes tagged "decorator" and "descriptor"http://code.activestate.com/recipes/tags/decorator+descriptor/2012-05-09T23:24:55-07:00ActiveState Code RecipesLazy Dynamic Binding on Classes (You'll Never Go Back) (Python)
2011-08-12T23:42:27-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577745-lazy-dynamic-binding-on-classes-youll-never-go-bac/
<p style="color: grey">
Python
recipe 577745
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/descriptors/">descriptors</a>).
Revision 4.
</p>
<p>This recipe provides a descriptor class and a decorator to make the deferred binding. Before the first access to that name, the __dict__ of the class will have the descriptor object. After that first access it will have whatever was returned by the first access.</p>
<p>One big advantage of deferring the binding is that the class object will be available at that time and can be passed to the object. During normal class creation the class body is exec'd before the class object is created, so the objects bound there can't be passed the class.</p>
<p><a href="http://code.activestate.com/recipes/577746/">Recipe #577746</a> provides a concrete example of how the deferred_binder can be used.</p>
Item Properties (Python)
2012-05-09T23:24:55-07:00Ian Kellyhttp://code.activestate.com/recipes/users/4178016/http://code.activestate.com/recipes/577703-item-properties/
<p style="color: grey">
Python
recipe 577703
by <a href="/recipes/users/4178016/">Ian Kelly</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/property/">property</a>).
Revision 2.
</p>
<p>A property variation that allows property access using an index or key.</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>
Docstring inheritance decorator (Python)
2009-07-28T13:42:32-07:00Shai Bergerhttp://code.activestate.com/recipes/users/2014324/http://code.activestate.com/recipes/576862-docstring-inheritance-decorator/
<p style="color: grey">
Python
recipe 576862
by <a href="/recipes/users/2014324/">Shai Berger</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/docstring/">docstring</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/metaprogramming/">metaprogramming</a>).
</p>
<p>In many cases, a subclass overrides a method in a parent class, just to change its implementation; in such cases, it would be nice to preserve the overridden method's docstring. The decorator below can be used to achieve this without explicit reference to the parent class. It does this by replacing the function with a descriptor, which accesses the parent class when the method is accessed as an attribute.</p>