Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?) Date: Wed, 23 Mar 2016 01:31:37 +1100 Lines: 66 Message-ID: References: <56e44258$0$1598$c3e8da3$5496439d@news.astraweb.com> <8737rvxs89.fsf@elektro.pacujo.net> <56e7483d$0$1608$c3e8da3$5496439d@news.astraweb.com> <56f09973$0$1601$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de jPnGf/Zv0vfb/PF03f+SUQ79W2c1ePC7aYgDFI/0ImMw== 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; 'else:': 0.03; 'clause': 0.07; 'differently': 0.07; 'false,': 0.07; 'filename': 0.07; 'cc:addr:python-list': 0.09; 'changed;': 0.09; 'filename,': 0.09; 'loop.': 0.09; 'subject:which': 0.09; 'exception': 0.13; 'ignore': 0.14; 'wed,': 0.15; 'file,': 0.15; '"":': 0.16; '"file': 0.16; '"is': 0.16; '(filename,': 0.16; '(ioerror,': 0.16; '2016': 0.16; '23,': 0.16; 'clause,': 0.16; 'e))': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'mean,': 0.16; 'oserror)': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:?)': 0.16; 'such,': 0.16; 'vanishingly': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'basically': 0.18; 'mechanism': 0.18; 'try:': 0.18; 'input': 0.18; 'changes': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'skip:" 30': 0.20; '%s"': 0.22; 'beauty': 0.22; 'exceptions': 0.22; 'level,': 0.22; 'terminate': 0.22; 'trying': 0.22; 'am,': 0.23; 'decide': 0.23; '(or': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.24; 'sense': 0.26; 'error': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'values': 0.28; '(maybe': 0.29; 'now?': 0.29; 'handled': 0.29; 'reporting': 0.29; 'print': 0.30; 'code': 0.30; "i'd": 0.31; 'aside': 0.32; 'though,': 0.32; 'useful': 0.33; 'problem': 0.33; 'traceback': 0.33; 'case,': 0.34; 'file': 0.34; 'except': 0.34; 'handle': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'text': 0.35; 'returning': 0.35; "isn't": 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'depends': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'received:209': 0.38; 'names': 0.38; 'mean': 0.38; 'end': 0.39; 'means': 0.39; 'does': 0.39; "didn't": 0.39; 'still': 0.40; 'some': 0.40; 'your': 0.60; 'subject:The': 0.61; 'skip:u 10': 0.61; 'here.': 0.62; 'course': 0.62; 'times': 0.63; 'between': 0.65; 'mar': 0.65; 'fall': 0.66; 'chrisa': 0.84; 'conditions,': 0.84; 'distinguish': 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; bh=7MQKTjIOnxCKIk66FPmIe4Kh6SNdWcjVQdoN0WO89+M=; b=njlX5FYEmxyptGWDWD4g7/dOkrB7BqII9q/hYy5qxVG9XdK0LJ9eiZtZnLD1S6SjUY u4UozmAoc6e4YhLxinvKehZl2gmouog7WkvyCCqEH2GUUH1kAQvZ6tCVFbIJ+2F/YVdz fxV+/oj7YTCHpoyQEF2qn8jM5Joh0H2c7MsKmzWBPQY50+IqpnokvW3q5CY2r6A/82+l uw5mVLvvH2kWZDQXrB4QEVf4783W1jn4enK/bdaZNy/MXbjUFqLCOt3yWYcibfKaQZi/ I5C9ZkZdgbqIsYWlPLBgKqrqQmEpn2Esd8v7f6atf065/ogxvDF7bnOzJ7QwvKSpYx5E FgZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=7MQKTjIOnxCKIk66FPmIe4Kh6SNdWcjVQdoN0WO89+M=; b=GQtasx8j6Pe9uXDD3NmB41MLXYZ9I63H4nNvWhqhVl+xkpqWSukQLmJKmOaFG1Dge1 M8nETPJeJvDD/gucBsl5L3arCGSPg5z5S9GraOWB+PHmJAhK+KrdJWD1sRUNPhmEDxy9 jefIbuGoxwinvUrV00pA++fMtUY76kgVtvP4Dt+K4whbOGwq4HnBZYzZL0MX/Syb2Sby EZn71MsrBlO3+/wpqApFEWBUVMO1N+1CbhXJp/6fRXkEo0Ut5sH69HoBYavoAA7rQm9H 63oP+pXh3upSxiRAoFsh1m65lZvOBu7Z0oKJckJadnAnMHpHN7J9gXuRhWhH7Ni9lgLJ C5Dw== X-Gm-Message-State: AD7BkJKVo+t16NeP08DSsVI0j2yvgVukO2VTLFcQUt+kI8wgPOXAF0/tkrwVT3zB7f1KoHoPn2SFXqsL6lEviA== X-Received: by 10.112.119.144 with SMTP id ku16mr13113577lbb.6.1458657097501; Tue, 22 Mar 2016 07:31:37 -0700 (PDT) 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:105492 On Wed, Mar 23, 2016 at 1:02 AM, BartC wrote: >> Note, though, that "deal with" really means "deal with", and NOT >> "print oops to the console and terminate". If all you're going to do >> with an exception is print and terminate, *let it go*, unless it's a >> user-triggered failure, in which case you catch it right at the very >> highest level, and basically fall off the end of your program. The >> number of times you have sys.exit() in an except clause should be >> vanishingly small. > > > So code like the following is consigned to history? > > while 1: > | file = input ("Filename (press enter to quit)? ") > | if file == "": break > | print ("Trying to open:",file,"...") > | s = readstrfile(file) > | if s != 0: > | | print (" ",file,"has",len(s),"characters") > | else: > | | print (" There was a problem opening:",file) > > (Oops, ignore the bars.) Your "else" clause there conceals the fact that it's actually *recovering* from an error condition, by reporting it and then returning to the main loop. So here's how I'd write it: while "moar files": filename = input("Filename (blank to quit): ") if not filename: break print("Reading %s..." % filename, end="\r") try: print(" %s has %d characters." % (filename, len(readfile(filename)))) except (IOError, OSError) as e: print(" Cannot read %s: %s" % (filename, e)) Aside from a few trivial changes to names and text strings and such, the one significant change is that I catch just two types of error (I/O and OS), and *I print out the error message*. In this case, the traceback wouldn't be very helpful, so it's dropped; but the text message is useful *to the user*, not just the programmer. If I were using a program like this and it didn't distinguish between "file not found", "permission denied", "is a directory", and "disk read failure", I would be extremely displeased. Note that I didn't catch UnicodeDecodeError here. If the file you're trying to read isn't a text file, the exception will terminate the process. Perhaps you decide that this should be handled too; all you have to do is add it to the existing except clause, or add a second one to handle this differently (maybe just "except UnicodeDecodeError: print('is binary')"). That's the beauty of exception handling; you handle as many or as few types as make sense for you to handle. > And, forgetting file input for a minute, what about function return values > in general; should they still be allowed to return some status or error > codes, or does it all have to be exceptions now? > > I mean, is a function allowed to still return True or False, or just False? > (Or perhaps just nothing if the exception mechanism can signal either.) Depends on what those return values mean! If they mean exceptional conditions, perhaps they should be changed; if they mean return values, of course they shouldn't. ChrisA