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


Groups > comp.lang.python > #95723

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

Path csiph.com!goblin1!goblin.stu.neva.ru!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <lac@openend.se>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'float': 0.05; 'subject:number': 0.07; 'received:openend.se': 0.09; 'received:theraft.openend.se': 0.09; 'subject:both': 0.09; 'assume': 0.11; 'things.': 0.15; 'from:addr:lac': 0.16; 'from:addr:openend.se': 0.16; 'from:name:laura creighton': 0.16; 'reason.': 0.16; 'recognise': 0.16; 'subject:)?': 0.16; 'laura': 0.18; 'work,': 0.21; 'parsing': 0.22; 'slightly': 0.23; 'somewhere': 0.24; 'decimal': 0.29; 'received:se': 0.29; 'code': 0.30; 'somebody': 0.30; 'probably': 0.31; 'generally': 0.32; 'problem': 0.33; 'raising': 0.33; 'feed': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'should': 0.36; 'lines': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'charset:us- ascii': 0.37; 'things': 0.38; 'to:addr:python.org': 0.40; 'your': 0.60; 'header:Message-Id:1': 0.61; 'here.': 0.62; 'more': 0.63; 'different': 0.63; 'header:In-reply-to:1': 0.84; 'subject:Casting': 0.84
To python-list@python.org
From Laura Creighton <lac@openend.se>
Subject Re: Casting to a "number" (both int and float)?
In-reply-to <0bdda01a-de29-4742-9851-0617dad602ae@googlegroups.com>
References <0bdda01a-de29-4742-9851-0617dad602ae@googlegroups.com>
Comments In-reply-to Victor Hooi <victorhooi@gmail.com> message dated "Thu, 27 Aug 2015 21:57:34 -0700."
MIME-Version 1.0
Content-Type text/plain; charset="us-ascii"
Content-ID <8183.1440741545.1@theraft.openend.se>
Date Fri, 28 Aug 2015 07:59:05 +0200
X-Greylist Sender IP whitelisted, not delayed by milter-greylist-4.3.9 (theraft.openend.se [127.0.0.1]); Fri, 28 Aug 2015 07:59:07 +0200 (CEST)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.98.1440741549.11709.python-list@python.org> (permalink)
Lines 18
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1440741549 news.xs4all.nl 23847 [2001:888:2000:d::a6]:50680
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:95723

Show key headers only | View raw


I suspect your code will have these 2 lines in it somewhere ...

if isinstance(field, dict):
   return int(field['floatApprox'])

using isinstance() or type() is generally frowned upon because
it breaks duck typing, and makes it necessary for you to write more
code every time somebody wants to feed your code slightly different
arguments.  Your code breaks for no particularly good reason.

But that's not your problem here.  You are parsing things.  And right
now it is your job to know exactly what type things are -- that _is_
the job.  Should somebody start feeding you field['Money'] you don't
want things to quietly assume that pretending it is a float will work,
when you really want a Decimal -- raising a TypeError about 'I don't
recognise this type' is probably what you want to do here.

Laura

Back to comp.lang.python | Previous | NextPrevious 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