Python Idiom Question

sburrious sburr at home.com
Tue Jul 10 22:31:47 EDT 2001


"Steve Holden" <sholden at holdenweb.com> wrote in message news:<mks27.2166$xf7.122975 at e420r-atl3.usenetserver.com>...
> "Steve Holden" <sholden at holdenweb.com> wrote in message
> news:8Zr27.2394$vA6.115437 at e420r-atl1.usenetserver.com...
> > "Tim Daneliuk" <tundra at tundraware.com> wrote in ...
> > > What is the equivalent python idiom to the following 'C' code:
> > >
> > > while (x=getchar() != 'N')
> > > {
> > > Do something}
> > >
> > > In other words, is there a convenient way to simultaneously get
> > > input, from say, raw_input() or f.read(), and check the return
> > > value agains some logical construct, re pattern match, or whatever?
[snip]
> > The approved idiom is
[snip]
> while 1:
>     x = raw_input()
>     if not x:
>         break
>     DoSomethingInteresting()

And others posted similar responses.

But you _can_ call raw_input in the test:

# argument_clinic.py
import re
response = re.compile(r'[Yy]es.*is[!.]?')
prompt = "No it isn't!  "
while 1:
    if response.match(raw_input(prompt)):
        pass # or "Do something interesting"
    else:
        print "I'm sorry, your five minutes are up."
        break

$ python argument_clinic.py
No it isn't!  yes it is
No it isn't!  Yes it is!
No it isn't!  Yes, it most certainly is!
No it isn't!  Look, this isn't an argument
I'm sorry, your five minutes are up.

Is there anything wrong or dangerous about this, apart from being
non-idiomatic?  I think it reads OK, not that my judgment is worth
much.

And, yes, I know I've got the roles reversed.  At least it's not
another example based on the Viking sketch.



More information about the Python-list mailing list