<br><br><div class="gmail_quote">On 30 July 2010 11:39, Nick Coghlan <span dir="ltr"><<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">On Fri, Jul 30, 2010 at 8:46 AM, Éric Araujo <<a href="mailto:merwok@netwok.org">merwok@netwok.org</a>> wrote:<br>
> Thank you for explaining generic functions so clearly.<br>
><br>
> Is there a good module out there implementing them without “crazily<br>
> complex overloading schemes”?<br>
<br>
</div>I'm not sure. Most of my exposure to generic functions is through PJE<br>
and he's a big fan of pushing them to their limits (hence RuleDispatch<br>
and PEAK-rules).<br>
<br>
There is an extremely bare bones implementation used internally by<br>
pkgutil's emulation of the standard import process, but Guido has said<br>
we shouldn't document or promote that in any official way without a<br>
PEP (cf. the simple proposal in <a href="http://bugs.python.org/issue5135" target="_blank">http://bugs.python.org/issue5135</a> and<br>
the PJE's previous more comprehensive proposal in PEP 3124).<br>
<br>
As others have explained more clearly than I did, generic functions<br>
work better than magic methods when the same basic operation (e.g.<br>
pretty printing, JSON serialisation) is common to many object types,<br>
but the details may vary between applications, or even within a single<br>
application. By giving the application more control over how different<br>
types are handled (through the generic functions' separate type<br>
registries) it is much easier to have context dependent behaviour,<br>
while still fairly easily sharing code in cases where it makes sense.<br>
<br>
E.g. to use Alex Gaynor's example of attendee list serialisation and<br>
the issue 5135 syntax:<br>
<br>
@functools.simplegeneric<br>
def json_unauthenticated(obj):<br>
    return json.dumps(obj) # Default to a basic dumps() call<br>
<br>
@functools.simplegeneric<br>
def json_authenticated(obj):<br>
    return json_unauthenticated(obj) # Default to being the same as<br>
unauthenticated info<br>
<br>
@json_unauthenticated.register(EventAttendees):<br>
def attendee_titles(attendees):<br>
    return json.dumps([attendee.title for attendee in attendees])<br>
<br>
@json_authenticated.register(EventAttendees):<br>
def attendee_details(attendees):<br>
    return json.dumps([attendee.full_details() for attendee in attendees])<br>
<br></blockquote><div><br><br>I really like Alex Gaynor's simple MultiMethod implementation. From:<br><br><a href="http://alexgaynor.net/2010/jun/26/multimethods-python/">http://alexgaynor.net/2010/jun/26/multimethods-python/</a><br>
<br>It doesn't have a concept of a default call, but that would be very easy to add. Basic usage is:<br>
<br>json_unauthenticated = MultiMethod()<br><br>
@json_unauthenticated.register(EventAttendees)<br>def json_unauthenticated(attendees):<br>    return json.dumps([attendee.title for attendee in attendees])<br><br>@json_unauthenticated.register(OtherType)<br>
def json_unauthenticated(othertypes):<br>
    return json.dumps(othertypes)<br><br>And so on.<br><br>Michael<br><br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
(Keep in mind that I don't use JSON, so there are likely plenty of<br>
details wrong with the above, but it should give a basic idea of what<br>
generic functions are designed to support).<br>
<div class="im"><br>
Cheers,<br>
Nick.<br>
<br>
--<br>
Nick Coghlan   |   <a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>   |   Brisbane, Australia<br>
_______________________________________________<br>
</div><div><div></div><div class="h5">Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><a href="http://www.voidspace.org.uk">http://www.voidspace.org.uk</a><br><br><br>