[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Why does list have no 'get' method?

Denis Bilenko

2/7/2008 4:01:00 PM

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.
4 Answers

nn

2/7/2008 5:15:00 PM

0

On Feb 7, 11:01 am, "Denis Bilenko" <denis.bile...@gmail.com> wrote:
> 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.

This has been a pet peeve of mine too. So you are not completely
crazy:

http://aspn.activestate.com/ASPN/Cookbook/Python/Rec...

There is a discussion about precisely this by Alex Martelli in the
printed version of the Python cookbook (2nd edition). I don't share
the opinion but I am not going to fight the core Python developers
over it. I don't think it is worth the effort. Python is so smooth
already, there has to be some excuse to add some cute hack to your
program once in a while :-).

dict(enumerate(lst)).get(i,default) for example although not very
efficient, looks cute. I think I like it better than my own solution.

nn

2/7/2008 7:51:00 PM

0

On Feb 7, 12:15 pm, prueba...@latinmail.com wrote:
> On Feb 7, 11:01 am, "Denis Bilenko" <denis.bile...@gmail.com> wrote:
>
>
>
> > 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.
>
> This has been a pet peeve of mine too. So you are not completely
> crazy:
>
> http://aspn.activestate.com/ASPN/Cookbook/Python/Rec...
>
> There is a discussion about precisely this by Alex Martelli in the
> printed version of the Python cookbook (2nd edition). I don't share
> the opinion but I am not going to fight the core Python developers
> over it. I don't think it is worth the effort. Python is so smooth
> already, there has to be some excuse to add some cute hack to your
> program once in a while :-).
>
> dict(enumerate(lst)).get(i,default) for example although not very
> efficient, looks cute. I think I like it better than my own solution.

Actually to clarify, I think this is more useful for tuples (or tuple
like structures) than lists per se. On homogeneous lists I usually
iterate. It is on heterogeneous that I want to extract specific fields
that might be optional.

Paul Rubin

2/7/2008 9:36:00 PM

0

"Denis Bilenko" <denis.bilenko@gmail.com> writes:
> 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()

I haven't looked at the original file but the above really looks like
it should use an iterator instead.

r

1/19/2013 5:01:00 PM

0

In article <vfalf853qq4jiqsicjc1huqv7e7ftsts7c@4ax.com>, duke
<duckgumbo32@cox.net> wrote:

> On Fri, 18 Jan 2013 15:31:02 -0800, r@somis.org (?RLMeasures) wrote:
>
> >In article <1u4jf8t3p27r3i9sjgregbc2elu21hfrh7@4ax.com>, duke
> ><duckgumbo32@cox.net> wrote:
> >
> >> On Thu, 17 Jan 2013 05:36:03 -0800, r@somis.org (?RLMeasures) wrote:
> >>
> >> >In article <4fuff8tf6j0ulrpm3vmgk4560b1nt50trq@4ax.com>, duke
> >> ><duckgumbo32@cox.net> wrote:
> >> >
> >> >> On Wed, 16 Jan 2013 17:25:15 -0800, r@somis.org (?RLMeasures) wrote:
> >> >>
> >> >> >In article <otldf85gc3fdgh79qipaect8ebv00ructo@4ax.com>, duke
> >> >> ><duckgumbo32@cox.net> wrote:
> >> >> >
> >> >> >> On Tue, 15 Jan 2013 15:34:26 -0800, r@somis.org (?RLMeasures) wrote:
> >> >> >>
> >> >> >> >In article <6d3bf8p75nu47lb5iddoc9dueq8j9ngb5f@4ax.com>, duke
> >> >> >> ><duckgumbo32@cox.net> wrote:
> >> >> >> >
> >> >> >> >> On Mon, 14 Jan 2013 18:04:35 -0500, James
> ><1rilu2@windstream.net> wrote:
> >> >> >> >>
> >> >> >> >> >I agree. The Bible says that God never had a beginning. So God
> >> >> >> >> >couldn't be "firstborn" . But according to the context of Col
> >1:15,16,
> >> >> >> >> >Jesus was the firstborn of things created.
> >> >> >> >>
> >> >> >> >> Yes, Son of God/one in God but not son of man.
> >> >> >> >> ...
> >> >> >> >
> >> >> >> >? just ignore that he referred to himself as "The Son of man".
> >> >> >>
> >> >> >> Why? It's the major mission of Jesus Christ.
> >> >> >>
> >> >> >? They why do you say not the Son of man.
> >> >>
> >> >> We, you and I, are "son of man". Jesus is Son of God who emptied
> >himself to
> >> >> became "son of man" only as example in flesh to other flesh the way
> >to eternal
> >> >> salvation.
> >> >>
> >> >? why do you say not the Son of man yet you say he became the son
of man??
> >>
> >> Stop and read.
> >
> >? I did.
>
> No idea what your confusion is.


? that's because there is none.