Casting to a "number" (both int and float)?

Jussi Piitulainen harvesting at makes.address.invalid
Fri Aug 28 08:15:09 CEST 2015


Ben Finney writes:

> Victor Hooi writes:
[- -]
>> For example:
>>
>> {
>>     "hostname": "example.com",
>>     "version": "3.0.5",
>>     "pid": {
>>         "floatApprox": 18403
>>     }
>>     "network": {
>>         "bytesIn": 123123,
>>         "bytesOut": {
>>             "floatApprox": 213123123
>>         }
>> }

[- -]

> In JSON there is no distinction at all, the only numeric type is
> ‘float’. What information is there in the input that can be used to
> know which values should result in an ‘int’ instance, versus values
> that should result in a ‘float’ instance?

I seem to get ints in the example data.

    >>> json.load(io.StringIO('{"floatApprox":31213}'))
    {'floatApprox': 31213}


More information about the Python-list mailing list