[Python-3000] Wither PEP 335 (Overloadable Boolean Operators)?
Neil Toronto
ntoronto at cs.byu.edu
Sat May 19 19:12:28 CEST 2007
Giovanni Bajo wrote:
> On 19/05/2007 3.21, Guido van Rossum wrote:
>
>>>> While reviewing PEPs, I stumbled over PEP 335 ( Overloadable Boolean
>>>> Operators) by Greg Ewing.
>>>>
>>>> It is time to reject it due to lack of interest, or revive it!
>>>>
>>> Didn't you post something about this a short time ago,
>>> suggesting you were in favour of it?
>>>
>> I think I did, but I hope I'm not the only one in favor.
>>
>
> I'm -0 on the idea, they're very rarely overloaded in C++ as well, since there
> are only few really valid use cases.
>
> In fact, the only example I saw till now where those of constructing
> meta-languages using Python's syntax, which is something that Python has never
> really encouraged (see the metaprogramming syntax which is now officially vetoed).
>
> But I'm not -1 because I assume that (just like unicode identifiers) they will
> not be abused by the community, and they probably do help some very rare and
> uncommon use cases where they are really required.
>
There's a fairly common one, actually, that comes up quite a lot in
Numpy. Currently, best practice is a wart. Here's some code of mine for
evaluating log probabilities from the Multinomial family:
class Multinomial(DistFamily):
@classmethod
def logProb(cls, x, n, p):
x = scipy.asarray(x)
n = scipy.asarray(n)
p = scipy.asarray(p)
result = special.gammaln(n + 1) - special.gammaln(x +
1).sum(-1) + (x * scipy.log(p)).sum(-1)
xsum = x.sum(-1)
psum = p.sum(-1)
return scipy.where((xsum != n) | (psum < 0.99999) | (psum >
1.00001) | ~scipy.isfinite(result), -scipy.inf, result)
That last bit is really confusing to new Numpy users, especially
figuring out how to do it in the first place. (Once you get it, it's not
*so* bad.) The parenthesis are required, by the way. With overloadable
booleans, it would become much more readable and newbie-friendly:
return scipy.where(xsum != n or psum < 0.99999 or psum >
1.00001 or not scipy.isfinite(result), -scipy.inf, result)
This isn't just an issue with "where" though - boolean arrays come up
quite a bit elsewhere, especially in indexing (you can index an array
with an array of booleans) and counting. Given that we're supposed to
see tighter integration with Numpy, I'd say this family of use cases is
fairly significant.
Neil
More information about the Python-3000
mailing list