[Tutor] Why begin a function name with an underscore

eryksun eryksun at gmail.com
Tue Aug 28 14:51:20 CEST 2012


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. But it's not fool-proof.
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

http://docs.python.org/library/abc


More information about the Tutor mailing list