The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

Chris Angelico rosuav at gmail.com
Sun Mar 20 22:07:55 EDT 2016


On Mon, Mar 21, 2016 at 1:04 PM, BartC <bc at freeuk.com> wrote:
> On 21/03/2016 01:35, Chris Angelico wrote:
>>
>> On Mon, Mar 21, 2016 at 12:28 PM, Mark Lawrence <breamoreboy at yahoo.co.uk>
>> 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?

They are. So would NameError, AttributeError, KeyboardInterrupt, and
anything else that happens to get raised. Everything gets absorbed
into the same message, "Can't open file", and then the program exits 0
to make absolutely sure that the caller can't figure anything out.

ChrisA



More information about the Python-list mailing list