Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95733

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

References <0bdda01a-de29-4742-9851-0617dad602ae@googlegroups.com> <mailman.96.1440740667.11709.python-list@python.org> <lf5wpwgj5w2.fsf@ling.helsinki.fi> <b172ba55-2188-4f83-ac5e-1311e9a51428@googlegroups.com>
Date 2015-08-28 19:23 +1000
Subject Re: Casting to a "number" (both int and float)?
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.107.1440753847.11709.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Aug 28, 2015 at 5:56 PM, Victor Hooi <victorhooi@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.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Casting to a "number" (both int and float)? Victor Hooi <victorhooi@gmail.com> - 2015-08-27 21:57 -0700
  Re: Casting to a "number" (both int and float)? Victor Hooi <victorhooi@gmail.com> - 2015-08-27 22:04 -0700
    Re: Casting to a "number" (both int and float)? "Sven R. Kunze" <srkunze@mail.de> - 2015-08-28 18:09 +0200
      Re: Casting to a "number" (both int and float)? Robin Koch <robin.koch@t-online.de> - 2015-08-28 18:15 +0200
        Re: Casting to a "number" (both int and float)? "Sven R. Kunze" <srkunze@mail.de> - 2015-08-30 13:38 +0200
        Re: Casting to a "number" (both int and float)? Steven D'Aprano <steve@pearwood.info> - 2015-08-30 22:00 +1000
        Re: Casting to a "number" (both int and float)? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-30 13:55 +0100
  Re: Casting to a "number" (both int and float)? Ben Finney <ben+python@benfinney.id.au> - 2015-08-28 15:44 +1000
    Re: Casting to a "number" (both int and float)? Jussi Piitulainen <harvestingn@makes.address.invalid> - 2015-08-28 09:03 +0300
    Re: Casting to a "number" (both int and float)? Jussi Piitulainen <harvesting@makes.address.invalid> - 2015-08-28 09:15 +0300
      Re: Casting to a "number" (both int and float)? Victor Hooi <victorhooi@gmail.com> - 2015-08-28 00:56 -0700
        Re: Casting to a "number" (both int and float)? Chris Angelico <rosuav@gmail.com> - 2015-08-28 19:23 +1000
  Re: Casting to a "number" (both int and float)? Jussi Piitulainen <harvesting@makes.address.invalid> - 2015-08-28 08:46 +0300
  Re: Casting to a "number" (both int and float)? random832@fastmail.us - 2015-08-28 01:54 -0400
  Re: Casting to a "number" (both int and float)? Laura Creighton <lac@openend.se> - 2015-08-28 07:59 +0200

csiph-web