Path: csiph.com!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '(using': 0.07; 'subject:number': 0.07; 'cc:addr:python-list': 0.09; 'input,': 0.09; 'subject:both': 0.09; 'def': 0.13; 'api,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'json,': 0.16; 'pythonic': 0.16; 'subject:)?': 0.16; 'wrote:': 0.16; 'integer': 0.18; 'input': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'fairly': 0.22; 'are.': 0.22; 'converted': 0.22; 'code.': 0.23; 'bit': 0.23; 'seems': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'workaround': 0.29; "i'm": 0.30; 'minimal': 0.30; 'skip:s 30': 0.31; 'received:google.com': 0.35; 'acceptable': 0.35; 'path': 0.35; 'but': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'data': 0.39; 'field': 0.60; 'your': 0.60; 'chrisa': 0.84; "it'd": 0.84; 'subject:Casting': 0.84; 'victor': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=DVUQuUcFgDOBqfF6yoB4edN5+iwPKWm4SWGan7TCb74=; b=FrBorBcayyoMp4UAV+6RItB33o4mFK6IXUZhFyvSRDWoCy0M6K/RBVwN9iE/YUAQ6N Mc+wbmNxMk8Wmys2yG2795OGu+PwnsgmWeWws67OzotLsLl7pJ2MhXHs7HudCNtSQoq+ v1sPWxlNgLNckrx1I/iPPESR6/8z5OyNgYOzgcpb3nw+Q9a/7rKAFM0qJcxb+2XoeJfG J/lfagGaFm4yC9K7BgV5k4A4SIF0L4/9dTWPNLdXCG1IITVxGmCw+2/VnrIZ1e31nK7E teRrGz7bpO0RkIA7gIpunMyAQlWhi4GRAz/Hz8TqBg4zH6tcHF82hPktRzn4PWoiDXuO 0ucA== MIME-Version: 1.0 X-Received: by 10.50.118.104 with SMTP id kl8mr2568648igb.92.1440753839004; Fri, 28 Aug 2015 02:23:59 -0700 (PDT) In-Reply-To: References: <0bdda01a-de29-4742-9851-0617dad602ae@googlegroups.com> Date: Fri, 28 Aug 2015 19:23:58 +1000 Subject: Re: Casting to a "number" (both int and float)? From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 20 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1440753847 news.xs4all.nl 23768 [2001:888:2000:d::a6]:45592 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95733 On Fri, Aug 28, 2015 at 5:56 PM, Victor Hooi 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