[Tutor] Why begin a function name with an underscore
Peter Otten
__peter__ at web.de
Tue Aug 28 15:08:51 CEST 2012
eryksun wrote:
> On Tue, Aug 28, 2012 at 6:00 AM, Peter Otten <__peter__ at web.de> wrote:
>>
>> Anyway here's an alternative implementation:
>>
>>>>> def vi(x):
>> ... if not isinstance(x, numbers.Number):
>> ... raise TypeError
>> ... if not int(x) == x:
>> ... raise ValueError
>
> You could test against numbers.Integral.
That would reject "floats with an integral value" and therefore doesn't
comply with the -- non-existing -- spec.
> But it's not fool-proof.
Nothing is. The code attempts to make expectations more explicit than Steven
did in his original implementation.
> Someone can register an incompatible class with the abstract base
> class (ABC).
>
> >>> import numbers
> >>> isinstance("42", numbers.Integral)
> False
> >>> numbers.Integral.register(str)
> >>> isinstance("42", numbers.Integral)
> True
That's quite an elaborate scheme to shoot yourself in the foot ;)
More information about the Tutor
mailing list