Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Dennis Lee Bieber 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 18:59:38 -0400 Organization: IISS Elusive Unicorn Lines: 38 Message-ID: References: <8737rvxs89.fsf@elektro.pacujo.net> <56e7483d$0$1608$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de Nof1JPkNfUvmm/Q3vvVvmQ6xDrQs+ryxcwQ/mxcEUh5g== 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; 'string.': 0.04; 'caller': 0.07; 'except:': 0.07; '"unexpected': 0.09; 'eof': 0.09; 'ioerror:': 0.09; 'message-id:@4ax.com': 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; 'exception': 0.13; '"":': 0.16; '#empty': 0.16; '2016': 0.16; 'here"': 0.16; 'interest,': 0.16; 'pythonic': 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; 'sys.exit(0)': 0.16; 'string': 0.17; 'try:': 0.18; 'url:home': 0.18; '(in': 0.18; 'exceptions': 0.22; 'pass': 0.22; 'decide': 0.23; '(or': 0.23; 'mon,': 0.24; 'header:X-Complaints-To:1': 0.26; 'error': 0.27; 'raise': 0.29; 'read,': 0.29; 'print': 0.30; 'classes': 0.30; 'point': 0.33; 'problem': 0.33; 'case,': 0.34; 'except': 0.34; 'handle': 0.34; 'gets': 0.35; 'attempt': 0.35; 'happened': 0.35; 'returning': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'received:org': 0.37; 'charset :us-ascii': 0.37; "won't": 0.38; 'anything': 0.38; 'someone': 0.38; 'mean': 0.38; 'data': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'behavior': 0.61; 'subject:The': 0.61; 'more': 0.63; 'mar': 0.65; 'here': 0.66; 'completing': 0.72; 'dennis': 0.91; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: adsl-108-79-219-206.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES 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:105412 On Mon, 21 Mar 2016 19:43:23 +0000, BartC declaimed the following: > >Or does Pythonic mean bristling with exceptions and classes and what-not? What is not Pythonic is returning numeric 0 if ANYTHING prevented the read operation from completing successfully. Let the exception through so the caller can decide if they want to handle it. data = readFileStr(fileName) if type(data) == type(int) and data == 0: print "Something happened but I won't tell you what" sys.exit(0) is NOT Pythonic... try: data = readFileStr(fileName) if data == "": #empty string print "EOF on read, no data returned" except IOError: print "Unexpected IO Error -- data is emptied here" data = "" except: #only here for the print, normal behavior would pass it up print "Unhandled exception being reraised" raise is more Pythonic. It uses exception handlers, at the point of interest, in the attempt to "fix" the problem (in this case, treating any IO Error as if it were EOF on read -- as EOF on normal read returns an empty string. Any other error gets reraised so someone higher can handle it (or it kills the program at the very top level). -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/