[Python-Dev] mutable integers

Guido van Rossum guido@python.org
Mon, 16 Oct 2000 09:30:44 -0500

Rob W. W. Hooft:

> Just checking my E-mail coming back from holidays, I see:
>     def leapdays(y1, y2):
>         """Return number of leap years in range [y1, y2).
>   !        Assume y1 <= y2."""
>   !     y1 -= 1
>   !     y2 -= 1
>   !     return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400)
> I am worried about the use of "-=" here. The code basically says: "if the
> arguments are mutable numeric types, I'd like to modify them in place". In
> c.l.py it has been stressed multiple times that "-=" is not the same as
> "=   -". I think this is clearly a place where "=   -" is preferable over
> "-=". Even though you might think people will always call this with 
> python integers, you can never be sure enough.

Hmm...  Your fear sounds a little paranoid for this particular case,
but I can't say that I totally disagree in general.  This is
definitely something to keep in mind when writing polymorphic
functions for which a NumPy array might be an acceptable input

I don't think it's worth checking in a patch in this case.

--Guido van Rossum (home page: http://www.python.org/~guido/)