[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

On Fri, Aug 28, 2015 at 5:56 PM, Victor Hooi <victorhooi at gmail.com> wrote:
> Currently I'm using this:
> def strip_floatApprox_wrapping(field):
>     # Extracts a integer value from a field. Workaround for the float_approx wrapping.
>     if isinstance(field, dict):
>         return field['floatApprox']
>     else:
>         return field
> I was a little hesitant to go down that path (using isinstance()) since it seems a bit "un-Pythonic" but it seems to do what I want in a minimal amount of code.

It'd not be as Pythonic if this were an API, but since you just
converted this from JSON, you can be fairly confident of what the data
types are. You're dealing with your program's input, and that input
can come in a specific number of forms; so you cope with each form.
This is a perfectly acceptable and reasonable way to write code.