Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Mark Lawrence Newsgroups: comp.lang.python Subject: Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?) Date: Mon, 21 Mar 2016 03:59:37 +0000 Lines: 52 Message-ID: References: <56e44258$0$1598$c3e8da3$5496439d@news.astraweb.com> <8737rvxs89.fsf@elektro.pacujo.net> <56e7483d$0$1608$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de m7whe5DFhozliUjstm7SRwkYLVZKI6W5NvD/VOrF0zOQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'from:addr:yahoo.co.uk': 0.05; '21,': 0.07; 'except:': 0.07; '22,': 0.09; 'exceptions,': 0.09; 'mark.': 0.09; 'oh,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:which': 0.09; 'anyway': 0.11; 'exception': 0.13; 'def': 0.13; 'file,': 0.15; '2016': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:?)': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'memory': 0.17; 'string': 0.17; 'try:': 0.18; 'language': 0.19; '>>>': 0.20; 'load': 0.20; 'lawrence': 0.22; 'errors': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'chris': 0.26; 'actual': 0.28; 'print': 0.30; 'up.': 0.32; 'language.': 0.32; 'possibly': 0.32; 'returned': 0.32; 'url:python': 0.33; 'open': 0.33; 'file': 0.34; 'skip:d 20': 0.34; 'gets': 0.35; 'so,': 0.35; 'could': 0.35; 'generic': 0.35; 'but': 0.36; 'there': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'being': 0.37; 'received:org': 0.37; 'anything': 0.38; 'skip:p 20': 0.38; 'why': 0.39; 'goes': 0.39; 'data': 0.39; 'to:addr:python.org': 0.40; 'mark': 0.40; 'some': 0.40; 'url:3': 0.60; 'subject:The': 0.61; 'avoid': 0.61; 'charset:windows-1252': 0.62; 'more': 0.63; 'our': 0.64; 'mar': 0.65; 'better.': 0.66; 'picked': 0.66; 'results': 0.66; 'saw': 0.77; 'prefers': 0.84; 'promptly': 0.84; 'pythonistas,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 80.234.129.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105332 On 21/03/2016 02:04, BartC wrote: > On 21/03/2016 01:35, Chris Angelico wrote: >> On Mon, Mar 21, 2016 at 12:28 PM, Mark Lawrence >> wrote: >>> I got to line 22, saw the bare except, and promptly gave up. >> >> Oh, keep going, Mark. It gets better. >> >> def readstrfile(file): >> try: >> data=open(file,"r").read() >> except: >> return 0 >> return data >> >> def start(): >> psource=readstrfile(infile) >> if psource==0: >> print ("Can't open file",infile) >> exit(0) >> >> >> >> So, if any exception happens during the reading of the file, it gets >> squashed, and 0 is returned - which results in a generic message being >> printed, and the program terminating, with return value 0. Awesome! > > I don't have a clue about exceptions, but why wouldn't read errors be > picked up by the same except: block? If you don't understand exceptions, you don't understand Python, which prefers EAFP than LBYL. See https://docs.python.org/3/glossary.html#term-eafp and https://docs.python.org/3/glossary.html#term-lbyl > > But I've anyway sprinkled one or two more try/excepts in there and put > some actual exception codes in. However, this readstrfile() is just > there to load the file into memory and avoid having a 200,000-line > string in the program. > Just let the exception bubble up if anything goes wrong. In most circumstances that's far better than masking everything that could possibly go wrong. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence