Popular recipes tagged "class_factory"http://code.activestate.com/recipes/tags/class_factory/popular/2013-10-24T11:31:44-07:00ActiveState Code Recipesdynamically changing encoder (for json) with metaclass (class factory) (Python)
2013-10-24T11:31:44-07:00-http://code.activestate.com/recipes/users/4188267/http://code.activestate.com/recipes/578696-dynamically-changing-encoder-for-json-with-metacla/
<p style="color: grey">
Python
recipe 578696
by <a href="/recipes/users/4188267/">-</a>
(<a href="/recipes/tags/class_factory/">class_factory</a>, <a href="/recipes/tags/encode/">encode</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/metaclasses/">metaclasses</a>).
</p>
<p>The <em>json.dumps</em> need to be feed with some class (cls =someClass) but what if we want to change the class dynamically?
This example can be done by declaring the <em>listOfClasses</em> in class level of course, but the idea is to be changeable. This can be done by the class factory function <em>encoderFacory</em></p>
Simple, if naive, class factory recipe in python (Python)
2009-11-09T07:59:53-08:00Ariel Balterhttp://code.activestate.com/recipes/users/4172249/http://code.activestate.com/recipes/576951-simple-if-naive-class-factory-recipe-in-python/
<p style="color: grey">
Python
recipe 576951
by <a href="/recipes/users/4172249/">Ariel Balter</a>
(<a href="/recipes/tags/class_factory/">class_factory</a>).
Revision 2.
</p>
<p>I'm a hack programmer -- no formal education. So, I don't know if this is technically a "factory", "abstract factory" or something else.</p>
<p>It is a way to generate a class dynamically, perhaps based on run-time data.</p>
<p>The point is that you can take a blank class object and dynamically add a constructor, class attributes, and instance methods. In principle, these could be configured dynamically in a program.</p>
<p>Since I don't know anything about programming theory, I welcome any criticism/discussion/suggestion. But, please be gentle!</p>