Richard D. Moores wrote: > On Tue, Aug 28, 2012 at 1:21 AM, Timo <timomli...@gmail.com> wrote: >> Op 28-08-12 10:06, Richard D. Moores schreef: > >>> What if I wanted 3., 1234., etc. to be considered ints, as they are by >>> _validate_int() ? >> >> >>>>> isinstance(3., (int, float)) >> True >> >> Because 3. is a float, not int. > > And >>>> isinstance(3.7, (int, float)) > True > > No, I'm asking for something equivalent to _validate_int().
That's not how it works. You have to give a spec first and then you can compare how well it is met by a particular implementation. If you start with an implementation and then declare its behaviour to be the spec the implementation will always "win". That's why ISO 29500 works so well -- for the company that wrote it. Anyway here's an alternative implementation: >>> def vi(x): ... if not isinstance(x, numbers.Number): ... raise TypeError ... if not int(x) == x: ... raise ValueError ... >>> vi("") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in vi TypeError >>> vi(1.5) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 5, in vi ValueError >>> vi(1.0) >>> The differences to _validate_int() are subtle: >>> class S(str): ... def __eq__(self, other): return True ... def __ne__(self, other): return False ... def __add__(self, other): return self ... >>> vi(S("42")) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in vi TypeError >>> _validate_int(S("42")) >>> _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor