[Python-3000] defop ?
Antoine Pitrou
solipsis at pitrou.net
Wed Nov 22 23:56:03 CET 2006
Le mercredi 22 novembre 2006 à 12:47 -0800, Guido van Rossum a écrit :
> > class DefOpDescriptor:
> > def __init__(self, genfunc, implfunc):
> > self.genfunc = genfunc
> > self.implfunc = implfunc
> > def __call__(self, *args, **kargs):
> > # Directly call the implementation (why not)
> > self.implfunc(*args, **kargs)
>
> I'm not sure how/when __call__ would be used.
>
> Shouldn't you implement a __get__ descriptor instead?
Hmmm yes, thanks for pointing that out :-o. It will (or should) work
better like this:
def __get__(self, obj, objtype):
return types.MethodType(self.implfunc, obj, objtype)
Besides, if we don't want to allow direct calls of the implementation
without going through generic method dispath (see below), we don't need
__get__ at all.
> Aha, cool. You forgot to add that now the method name chosen must be
> unique. This works fine to describe @defop, and I like it better than
> using sys._getframe(). But it doesn't really work for the defop
> syntax, unless that makes up a unique (random?) name for each
> occurrence.
Indeed it could mangle a unique name, like __private does.
Also, DefOpDescriptors could be removed from the class dictionary at
class construction, and appended to a __generic_overloads__ attribute.
This would keep the class dict clean, and produce better documentation
by separating generic function implementations from other methods in the
documentation.
(it probably still sounds a bit hackish)
More information about the Python-3000
mailing list