check if object is number

John Lenton john at grulic.org.ar
Fri Feb 11 16:00:52 EST 2005


On Fri, Feb 11, 2005 at 01:17:55PM -0700, Steven Bethard wrote:
> George Sakkis wrote:
> >"Steven Bethard" <steven.bethard at gmail.com> wrote in message
> >news:gcidnb9g_ojxnpDfRVn-tA at comcast.com...
> >
> >>Is there a good way to determine if an object is a numeric type?
> >
> >In your example, what does your application consider to be numeric?
> 
> Well, here's the basic code:
> 
> def f(max=None):
>     ...
>     while max is None or n <= max:
>         ...
>         # complicated incrementing of n
> 
> So for 'max', technically all I need is <= support.  However, the code 
> also depends on the fact that after incrementing 'n' enough, it will 
> eventually exceed 'max'.  Currently, ints, longs, floats, and Decimals 
> will all meet this behavior.  But I'd rather not specify only those 4 
> (e.g. with a typecheck), since someone could relatively easily create 
> their own new numeric type with the same behavior.  Do you know a better 
> way to test for this kind of behavior?

Why don't you express just this need as an assertion?

    assert 0 <= max <= max + 1, 'Argument must not be zany'

(or whatever error message is suitable)

-- 
John Lenton (john at grulic.org.ar) -- Random fortune:
<Silvrbear> Oxymorons?  I saw one yesterday - the pamphlet on "Taco Bell
            Nutritional Information"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20050211/7d481d82/attachment.sig>


More information about the Python-list mailing list