Second python program: classes, sorting

Eric Brunel see.signature at no.spam
Mon Aug 11 10:51:02 CEST 2008


Others have replied to your original question. As an aside, just a few  
stylistic notes:

> class Score:
>      def __init__(self, name_, score_):
>          self.name = name_
>          self.score = score_

These trailing underscores look like a habit from another language. They  
are unneeded in Python; you can write:
class Score:
      def __init__(self, name, score):
          self.name = name
          self.score = score
That's an advantage of the explicit self: no ambiguity between local  
variables and attributes.

> try:
>      infile = open(filename, "r")
> except IOError, (errno, strerror):
>      print "IOError caught when attempting to open file %s. errno = %d,  
> strerror = %s" % (filename, errno, strerror)
>      exit(1)

This try/except block is unneeded too: what you do with the exception is  
more or less the same as the regular behaviour (print an error message and  
exit), except your error message is far less informative than the default  
one, and printed to stdout instead of stderr. I would remove the entire  
try/except block and just write:
infile = open(filename, "r")

HTH
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in  
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"



More information about the Python-list mailing list