Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!news.roellig-ltd.de!open-news-network.org!cyclone02.ams2.highwinds-media.com!news.highwinds-media.com!voer-me.highwinds-media.com!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone02.am1.xlned.com!bcyclone02.am1.xlned.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string.': 0.05; 'assignment': 0.07; 'string': 0.09; 'debugger': 0.09; 'except:': 0.09; 'exception,': 0.09; 'literal': 0.09; 'subject:using': 0.09; 'try:': 0.09; 'val': 0.09; 'valueerror:': 0.09; 'cc:addr:python- list': 0.11; 'def': 0.12; '(pdb)': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'itself,': 0.16; 'nameerror:': 0.16; 'pdb;': 0.16; 'somehow.': 0.16; 'subclass': 0.16; 'throw': 0.16; 'undefined.': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'looked': 0.18; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'paul': 0.24; 'cc:2**0': 0.24; 'defined': 0.27; 'header:In-Reply- To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; '"",': 0.31; 'routine': 0.31; 'writes:': 0.31; 'file': 0.32; 'class': 0.32; 'probably': 0.32; 'interface': 0.32; "we're": 0.32; '(most': 0.33; 'guess': 0.33; 'trouble': 0.34; 'skip:_ 10': 0.34; 'could': 0.34; 'received:google.com': 0.35; '14,': 0.36; "didn't": 0.36; 'pm,': 0.38; 'recent': 0.39; 'help,': 0.39; 'sure': 0.39; 'conversion': 0.61; 'field.': 0.61; 'making': 0.63; 'name': 0.63; 'mar': 0.68; 'invalid': 0.68; '10:': 0.84; '2015': 0.84; 'examining': 0.84; 'fail.': 0.84; 'to:none': 0.92; '***': 0.95 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=qrG1jNGLUQIgTFi3mUE/L5RITidOpdSUcxwQQ0isifo=; b=SyJG3u+h3qVJbZhj8K4xNdiIBnFwfeIyHU3MltN0pByjgr5aQYpsPbtfl5HMdFJHyx cDYkCenyvIWMIKCZQe9jEFfJwat3ch9EvU0JLGojPG5D62erCo1Gd7wP7YYyqygNycwr DaPFJ0F39NH8fmlAFA/i/palQN/QTly9Z40r/BvDTtuUx+6yPJpu7CMN4/jHuyfCe2l+ FaJGFwILRh5LfcDq75rD7h4l8h6AFdSW083MXCPVeggcVRpMgKYmsWaNaO0fXyxWfBeK vGmVFJUWC9HdE4QzOzkhGr2k+e63tfqSt4uAnPUgbJi02FrlpDGyYGIBKBn4WaVNo5Yo fN7g== MIME-Version: 1.0 X-Received: by 10.107.160.212 with SMTP id j203mr91212881ioe.43.1426302577638; Fri, 13 Mar 2015 20:09:37 -0700 (PDT) In-Reply-To: <877fuk71fz.fsf@nightsong.com> References: <877fuk71fz.fsf@nightsong.com> Date: Sat, 14 Mar 2015 14:09:37 +1100 Subject: Re: Odd ValueError using 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.19 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1426302586 news.xs4all.nl 2913 [2001:888:2000:d::a6]:51730 X-Complaints-To: abuse@xs4all.nl X-Received-Body-CRC: 2457648449 X-Received-Bytes: 5107 Xref: csiph.com comp.lang.python:87404 On Sat, Mar 14, 2015 at 1:33 PM, Paul Rubin wrote: > emile writes: >> *** NameError: name 'val' is not defined >> (Pdb) l >> 139 try: >> 140 val = round(float(decval),1) >> 141 except: >> 142 import pdb; pdb.set_trace() > > If 'float' or 'round' throw an exception, the assignment to 'val' never > happens, so 'val' is undefined. Try examining the value of 'decval' in > the debugger to see what is making the conversion fail. That's exactly what the OP did :) Trouble is, it didn't help, because it sure looked as if decval was the string '4'. My best guess was - and is - that it's not just a string. We're looking at an SQL interface routine here, so it may be that there's a string subclass that length-limits itself, on the assumption that it's going into a fixed-length database field. Let's see if I can recreate the OP's situation... >>> def Char(maxlen): class CharN(str): def __repr__(self): return repr(self[:maxlen]) CharN.__qualname__ = CharN.__name__ = "Char(%d)"%maxlen return CharN >>> four = Char(1)('41.700000000000003') >>> four '4' >>> int(four) Traceback (most recent call last): File "", line 1, in int(four) ValueError: invalid literal for int() with base 10: '4' ... well, close. Anyway, a string subclass could probably do this by accident somehow. ChrisA