[Python-3000] pep 3124 plans

Nick Coghlan ncoghlan at gmail.com
Mon Jul 23 15:09:53 CEST 2007


Greg Ewing wrote:
> Phillip J. Eby wrote:
>> I don't think that anybody's saying that unrestricted use of dynamism is 
>> good, or that it can't be abused.  However, the potential for abuse is 
>> no different.
> 
> I'm not talking about abuse. I'm only talking about using
> GFs the way they're meant to be used. There's more to
> think about in the presence of GFs even without any
> abuse.

GF's are already used all the time in Python - they're just called magic 
methods.

So I'll assume you're happy with the idea that if you want to analyse 
the expression:

   d[a+b]

statically in current Python, you need to look for __add__ and __radd__ 
methods on both 'a' and 'b' (assuming you know their types), and 
__hash__ and __eq__ methods on whatever type is returned from that 
operation, and then a __getitem__ method on the type of 'd' (again, 
assuming you already know it). In all cases, the methods might not 
actually be on those particular types, but on one of their parent types. 
And if there are any invocations of super() in any of the method 
implementations, then you need to take the MRO into account as well.

Of course, most of the time you wouldn't bother with that level of 
analysis unless you had reason to believe something was going wrong with 
that expression. Otherwise, you would assume that all of the magic 
methods involved were performing as expected.

So what's different if we change that expression to use GF's instead?:

   get_mapping_item(d, binary_add(a, b))

Well, nothing really, except that instead of looking for the magic 
methods referred to above, we are instead looking for all overloads of 
get_mapping_item and binary_add.

And the big benefit here is that whatever techniques you come up with 
for searching for those overloads will work for *any* GF implemented 
using the same tools, whereas the search for magic methods only works in 
some cases. For example, what would you need to search for to figure out 
what code copy.copy, copy.deepcopy, pickle.dumps or pickle.loads invoke 
for a given type? It's significantly more complicated than just looking 
for single magic methods.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list