My first Python program

Hallvard B Furuseth h.b.furuseth at
Tue Oct 12 22:14:52 CEST 2010

Seebs writes:

>        self.f = file(path, 'r')
>        if not self.f:
>            return None

No.  Failures tend to raise exceptions, not return error codes.
Except in os.path.exists() & co.

$ python
>>> open("nonesuch")   
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'nonesuch'

    import errno
        self.f = file(path, 'r')
    except IOError:
        if e.errno != errno.ENOENT: raise    # if you are picky
        return None


> if not section in self.sections:

  if section not in self.sections:

> list = map(lambda x:, self.args)
> return ', '.join(list)

  return ', '.join([ for x in self.args])

> self.type, = None, None

Actually you can write self.type = = None,
though assignment statements are more limited than in C.
(And I think they're assigned left-to-right.)

>  match = re.match('(.*)\(\*([a-zA-Z0-9_]*)\)\((.*)\)', text)

Make a habit of using r'' for strings with lots of backslashes,
like regexps.


More information about the Python-list mailing list