<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>What exactly are you hoping to accomplish?</div></blockquote><div><br></div><div>In brief, I wish there were a concise and clear way to express that an object implements a &quot;list of generator functions&quot;, in the context of CS students learning the concept of interface. </div>
<div><br></div><div>
I am writing an article on a sudoku solver for an audience of CS students, They do not know much of Python, have recently heard about recursivity and the yield keyword was not obvious for them. The goal is that they meet a nice collaboration between three objects, in Python, to manipulate the concepts of code reuse and interface. </div>
<meta http-equiv="content-type" content="text/html; charset=utf-8"><div><br></div><div>There is 1. a Sudoku class modelling a sudoku board and rules, </div><div>2. a backtracking algorithm,</div><div>3. and a function which returns the data structure (a list of generator functions) used by the backtracking algorithm, and which uses an instance of the Sudoku class,</div>
<meta http-equiv="content-type" content="text/html; charset=utf-8"><meta http-equiv="content-type" content="text/html; charset=utf-8">
<div><br></div><div>(The solver code can be read at <a href="http://jdb.github.com/modules/sudoku.html#Sudoku">jdb.github.com/modules/sudoku.html</a>, a draft of the article is at : <a href="http://jdb.github.com/sudoku.html">jdb.github.com/sudoku.html</a>, the nice backtracking algorithm comes from the conjoin algorithm described in test_generators.py in the Python sources)</div>
<meta http-equiv="content-type" content="text/html; charset=utf-8"><div><br></div><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div>As the naive implementation the students would write is one big fat function, I have to convince them that going through a more sophisticated design is a better path. The idea is to exemplify code reuse by swapping the Sudoku class with an optimized equivalent class, for instance. Also the same backtracking algorithm can &quot;cook&quot; a solution to the eight queens problems or the knight&#39;s tour problem, without modification but given the adapted list of generator functions.</div>
</div><div><br></div><div>The interfaces help designing formalizing the collaboration between objects and the article wishes to convey this idea. The Sudoku interface is nice and clean but explaining the interface for a &quot;list of generator functions&quot; is likely to raise more questions from the student than it brings clarity. The interface will require the students to know that &quot;list&quot; and its bracket notation implies the __getitem__ function, and that generator implies __iter__ and next(). Roughly a dozen elegant lines are needed to implement the interface with the Python keywords and syntax but the expected __getitem__, __iter__ and next() will be absent from the implementation which uses &quot;[]&quot; and &quot;yield&quot;. </div>
<div><br></div><div>I understand that the interfaces presented by Tres Seaver will work well for the purpose of code reuse in a real application, and it is the way to go if I want to use a global registry and/or a validation of the interface provided by a class. At this point, I think it is easier to explain the interface in plain english in the documentation with the help of the sphinx documentation directives extracting the public methods of the class.</div>
<meta http-equiv="content-type" content="text/html; charset=utf-8"><div><br></div><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div>Here is (maybe) what I wish I could write:</div><div><br></div>
<div>&quot;&quot;&quot;</div><div>from zope.interface import IList, IGenerator, Interface, provides, validates</div><div><br></div><div>def make_generator_functions():</div><div>    [...]</div><div><br></div><div>make_generator_functions = , make_generator_functions)</div>
<div><br></div><div>def stack_assumption(gen_funcs):</div><div>    validates(IList(IGenerator), gen_funcs)</div><div><br></div><div>    [ ... ]</div><div>&quot;&quot;&quot;</div></div><div><br></div><div>Thanks for your attention,</div>
<div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Jim<br>
<br>
--<br>
<font color="#888888">Jim Fulton<br>
</font></blockquote></div><br>