[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 9:58:00 AM

Tim Golden wrote:

> Dodging your question slightly (and at the risk of teaching
> my grandmother to suck eggs) I sometimes use this idiom for
> checking params. Obviously it only goes so far, but it's
> fairly compact:

> <Noddy example code>
> import os, sys

> if __name__ == '__main__':
> ARGS = None, "DEV"
> filename, db = > (j or i for i, j in map (None, ARGS, sys.argv[1:]))

> print sys.argv
> print filename, db

> </code>

Thank you for the example. It demonstrates perfectly how
much people miss this feature :)


Raymond Hettinger wrote:

> At first blush that example would make it seem like a good idea, but I
> don't see how the example could extend past the first index. If the
> port argument is optional, how would you know the index position of
> optional arguments to follow?

> With a dictionary, one could plausibly write:

> host = d.get('host', 'http://exampl...)
> port = d.get('port', 8080)
> path = d.get('path', '/')

> But would this make sense with a list:

> host = s.get(0, 'http://exampl...)
> port = d.get(1, 8080)
> path = d.get(2, '/')

> If positions 0 and 1 are optional, how do you expect to know whether
> "path" is going to be at position 2? This problem doesn't exist with
> dictionaries because the presence or absence of optional entries does
> not affect the key reference to other entries. Accordingly, I
> wouldn't expect that dict.get() would have a parallel list.get() with
> plausible use cases.

If you want to fill position 2, then positions 0 and 1 are mandatory.
It is the simplest possible option parsing, I didn't said it was the
most flexible :)

But perhaps it was a wrong example altogether.

Consider a couple more snippets, unrelated to command-line options.
(found by searching 'IndexError' in the python standard library)

this snippet from cmd.py:

try:
return self.completion_matches[state]
except IndexError:
return None

transforms into

return self.completion_matches.get(state)


another one from fileinput.py

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

becomes

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

both examples show reduction by 3 lines.

There's nothing dictionary-specific in 'get', it is
just a special use-case of '__getitem__' that is needed frequently.
Since list has '__getitem__' it deserves to have 'get' too.
8 Answers

Stefan Behnel

2/7/2008 10:43:00 AM

0

Denis Bilenko wrote:
> Raymond Hettinger wrote:
>> If positions 0 and 1 are optional, how do you expect to know whether
>> "path" is going to be at position 2? This problem doesn't exist with
>> dictionaries because the presence or absence of optional entries does
>> not affect the key reference to other entries. Accordingly, I
>> wouldn't expect that dict.get() would have a parallel list.get() with
>> plausible use cases.
>
> If you want to fill position 2, then positions 0 and 1 are mandatory.
> It is the simplest possible option parsing, I didn't said it was the
> most flexible :)

d = dict(enumerate(args))
print d[0], d[1], d.get(2)

Stefan

Paul Duca

1/19/2013 2:45:00 PM

0

On Saturday, January 19, 2013 1:23:45 AM UTC-5, harry k wrote:
> On Jan 18, 10:37 am, duke <duckgumb...@cox.net> wrote:
>
> > On Fri, 18 Jan 2013 08:25:11 -0800 (PST), harry k <turn...@q.com> wrote:
>
> > >On Jan 18, 4:35 am, duke <duckgumb...@cox.net> wrote:
>
> > >> On Thu, 17 Jan 2013 16:24:59 -0800, Jeanne Douglas <hlwdj...@NOSPAMgmail.com>
>
> > >> wrote:
>
> >
>
> > >> >In article <vrXJs.149530$lx1.111...@fed08.iad>,
>
> > >> > "Patrick" <PBARKER...@r.com> wrote:
>
> >
>
> > >> >> <SilentO...@hotmail.com> wrote >
>
> > >> >> > When condoms are properly used, the greatly reduce the transmission of
>
> > >> >> > HIV.
>
> >
>
> > >> >> When abstinence is used properly, ALL HIV is stopped completely.
>
> > >> >> You go for your reduction.  I'll vote for abstinence.
>
> > >> >Well, you're in a teensy tiny minority.
>
> >
>
> > >> And if you don't, HIV is in your life.
>
> >
>
> > >> >So how about something that people will actually do, instead of your
>
> > >> >silly fantasy of abstinence?
>
> >
>
> > >> The dukester, American - American
>
> > >> ********************************************
>
> > >> You can't fix stupid.
>
> > >> ********************************************
>
> >
>
> > >And just how is that abstinence thing working out to prevent teen
>
> > >pregnancies?
>
> >
>
> > Absolutely wonderful.  If you abstain, it right at impossible to get pregnant.
>
> >
>
> > You.........didn't know this?
>
> >
>
> > The dukester, American - American
>
> > ********************************************
>
> > You can't fix stupid.
>
> > ********************************************
>
>
>
> You do know what they call Catholics who use the rythm method?
>
>
>
> Harry K

Parents...

Paul

Paul Duca

1/19/2013 2:58:00 PM

0

On Saturday, January 19, 2013 9:28:35 AM UTC-5, Patrick wrote:
> "harry k" <turnkey@q.com> wrote in message
>
> news:fd85334c-b88c-4b1f-8340-01e82af5fa8b@d2g2000pbd.googlegroups.com...
>
> On Jan 18, 12:32 pm, "Patrick" <PBARKER...@r.com> wrote:
>
> > "harry k" <turn...@q.com> wrote
>
> >
>
> > And just how is that abstinence thing working out to prevent teen
>
> > pregnancies?
>
> >
>
> > + Not my business. Why do you feel it is yours?
>
>
>
> So you are too embarassed by the obvious you don't care to answer?
>
>
>
> http://abcnews.go.com/WN/study-abstinence-works/story?...

Can you show any evidence showing God gave then ANY reward in return? I mean, beyond "I'm not pregnant/diseased!" feelgood to parrot.

Paul

Paul Duca

1/19/2013 3:00:00 PM

0

On Saturday, January 19, 2013 9:32:17 AM UTC-5, Patrick wrote:
> "Free Lunch" <lunch@nofreelunch.us> wrote in message
>
> news:445kf81qme9n3d4g5b2s3os6l1uk37030h@4ax.com...
>
> > On Fri, 18 Jan 2013 18:56:49 -0500, "Patrick" <PBARKER001@r.com> wrote
>
> > in alt.atheism:
>
> >
>
> >>"Free Lunch" <lunch@nofreelunch.us> wrote in message
>
> >>news:likjf8dkmol5iogbbl6tods9t606e2rell@4ax.com...
>
> >>> On Fri, 18 Jan 2013 15:31:58 -0500, "Patrick" <PBARKER001@r.com> wrote
>
> >>> in alt.atheism:
>
> >>>
>
> >>>>"Jeanne Douglas" <hlwdjsd2@NOSPAMgmail.com> wrote in message
>
> >>>>news:hlwdjsd2-2A8317.16245817012013@news.giganews.com...
>
> >>>>> In article <vrXJs.149530$lx1.111899@fed08.iad>,
>
> >>>>> "Patrick" <PBARKER001@r.com> wrote:
>
> >>>>>
>
> >>>>>> <SilentOtto@hotmail.com> wrote >
>
> >>>>>> > When condoms are properly used, the greatly reduce the transmission
>
> >>>>>> > of
>
> >>>>>> > HIV.
>
> >>>>>>
>
> >>>>>> When abstinence is used properly, ALL HIV is stopped completely.
>
> >>>>>> You go for your reduction. I'll vote for abstinence.
>
> >>>>>
>
> >>>>> Well, you're in a teensy tiny minority.
>
> >>>>
>
> >>>>There are a billion catholcis on earth.
>
> >>>>
>
> >>>>
>
> >>>>
>
> >>>>> So how about something that people will actually do, instead of your
>
> >>>>> silly fantasy of abstinence?
>
> >>>>
>
> >>>>I have never considered abstinence a fantasy.
>
> >>>>I have always considered it a choice - a tough choice fer sure.
>
> >>>>
>
> >>> The priests and bishops have a long history of being no better at
>
> >>> abstinence than teenagers are.
>
> >>
>
> >>Should we give up then?
>
> >>Since we all know you are doomed to die, why try to live?
>
> >>
>
> > No, we should be reasonable in our expectations of the actions of
>
> > others.
>
>
>
> How well is that working out?
>
> Why do we keep building prisons?
>
> Why do our legislators keep passing new laws?
>
> Why do we continue to bury our murdered children?
>
>
>
>
>
> > We should not assume that all teens will remain abstinent just
>
> > because adults a generation older than them want them to be abstinent
>
> > and have forgotten how little abstinence there was back when they were
>
> > teens.
>
>
>
> So.... you are willing to throw in the towell?
>
> Since we know our kids will fuck around, give them condoms?

Patrick wishes HIS parents threw in the towel, instead of snapping it at him when he even touched himself.

Paul

Paul Duca

1/19/2013 3:05:00 PM

0

On Saturday, January 19, 2013 9:36:17 AM UTC-5, Patrick wrote:
> "harry k" <turnkey@q.com> wrote
>
>
>
> Where do you get "give up" from? We want the young to LEARN about
>
> their bodies, and how to avoid pregnancy.
>
>
>
> + That is a good start.
>
> + Pregnancy CAN be avoided by Abstinence.
>
> + Don't forget to teach this little factoid.

Congratulations on admitting your error...as any intelligent Catholic Republican knows, "factoid" is in truth an INACCURATE piece of information. The rabble is so stupid, it thinks it means a trivial but correct one.

Paul

Paul Duca

1/19/2013 3:06:00 PM

0

On Saturday, January 19, 2013 9:38:30 AM UTC-5, Patrick wrote:
> "harry k" <turnkey@q.com> wrote in message
>
> news:7b7f2a85-2c94-4343-9226-0b35581b92be@t6g2000pba.googlegroups.com...
>
> On Jan 18, 12:36 pm, "Patrick" <PBARKER...@r.com> wrote:
>
> > "duke" <duckgumb...@cox.net> wrote in message
>
> >
>
> > news:odgif8l1et5ft4ia5b3oheg9ge14lij4l0@4ax.com...
>
> >
>
> > > On Fri, 18 Jan 2013 00:32:00 -0500, SilentO...@hotmail.com wrote:
>
> >
>
> > >>On Thu, 17 Jan 2013 13:30:52 -0500, "Patrick" <PBARKER...@r.com>
>
> > >>wrote:
>
> >
>
> > >>><SilentO...@hotmail.com> wrote >
>
> > >>>> When condoms are properly used, the greatly reduce the transmission
>
> > >>>> of
>
> > >>>> HIV.
>
> >
>
> > >>>When abstinence is used properly, ALL HIV is stopped completely.
>
> > >>>You go for your reduction. I'll vote for abstinence.
>
> >
>
> > >>You've no effective means to persuade people to be abstinent
>
> >
>
> > > If they're not afraid of HIV, let 'em go.
>
> >
>
> > I was afraid of the incurable STD's that I was informed about
>
> > when I took my little tour to South East Asia. I kept straight.
>
>
>
> Is that an admission you were promiscuous whennot in S E Asia? What
>
> happened to that absinence thing?
>
>
>
> + Why do you assume the worst?
>
> + Oh.... that's right.... that's how I present myself.

Paul Duca

1/19/2013 3:14:00 PM

0

On Saturday, January 19, 2013 8:20:53 AM UTC-5, duke wrote:
> On Fri, 18 Jan 2013 16:53:32 -0600, Free Lunch <lunch@nofreelunch.us> wrote:
>
>
>
> >The priests and bishops have a long history of being no better at
>
> >abstinence than teenagers are.
>
>
>
> No priest has ever become pregnant as long as he abstains.
>
>
>


From what I remember from both biology class and Sunday school, that's unlikely even for a priest who works Times Square the other six days of the week.

Paul


Patrick

1/19/2013 4:23:00 PM

0

"Paul Duca" <paul.duca2@gmail.com> wrote
> Can you show any evidence showing God gave then ANY reward in return? I
> mean, beyond "I'm not pregnant/diseased!" feelgood to parrot.


Can you?