Not possible to hide local variables

Cecil Westerhof Cecil at decebal.nl
Wed Apr 29 08:25:50 CEST 2015


Op Tuesday 28 Apr 2015 10:06 CEST schreef Chris Angelico:

> In fact, it's not really your problem if someone gives you a length
> that isn't a simple integer. In the first place, they might give you
> a subclass of int, so a better check would be this:
>
> if not isinstance(length, int):
> raise ValueError("length should be integral")

Good tip: I implemented it.


> (note that I'm avoiding the multiple-argument syntax which doesn't
> work in Python 3;

I already did this with print. Are there other statements I have to
take care for the possible problems with Python 3? I do not use Python
3, but that does not mean it is not a good idea to make my modules
work with it.


> if ParameterError is a subclass of ValueError you
> could use that instead, but ValueError works)

Was the wrong error: should have been TypeError. I find this strange:
Python is compiled, but I get only an error when this statement is
reached.


> You don't even need to check that it's an integer. If you get
> something that can't be compared against 0, they'll get an instant
> exception; otherwise, the exception might come up later. But it's
> still not your problem. You could skip the nonnegativity check too,
> but that's good documentation. (I'm assuming that ought to be
> checking 'length', as there is no 'n' in your example.)

Yep, silly mistake. I now check for greater or equal two. A moving
average of length one is not very useful.

I am of the school that it is good to check as much as possible.
(Without going over the top.)

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof



More information about the Python-list mailing list