Why does list have no 'get' method?

Denis Bilenko denis.bilenko at gmail.com
Thu Feb 7 17:01:02 CET 2008


Steve Holden wrote:
> These versions differ with respect to treatment of blank lines, which
> indicates  how easy it is to go astray in this kind of semantic
> optimization. Your example simply wouldn't work (though you could patch
> it up using "if line is None". (despite the use of short-circuiting
> predicates).

> > both examples show reduction by 3 lines.
> >
> Perhaps so, but you have to realise that Python has never valued code
> compactness over clarity.

> I'm not sure that your democratic wish to ensure fairness to sequences
> will garner much support, interesting though it is in an academic sense.

Thank you for the patch. My incentives are not academic though.

I convinced that this

    line = self._buffer.get(self._bufindex)
    if line is None:
        self._bufindex += 1
        self._lineno += 1
        self._filelineno += 1
        return line
    line = self.readline()

is more clear than

    try:
        line = self._buffer[self._bufindex]
    except IndexError:
        pass
    else:
        self._bufindex += 1
        self._lineno += 1
        self._filelineno += 1
        return line
    line = self.readline()

I mentioned 3 lines reduction just because it is
objective, while 'more clear' is subjective.

Code snippets are again not equivalent, e.g. in the
case when self._buffer[self._bufindex] exists and is
equal to None. Although the author could probably
make a guarantee that is never the case.



More information about the Python-list mailing list