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: 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 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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