Python Idiom Question

Steve Holden sholden at holdenweb.com
Tue Jul 10 02:29:20 CEST 2001


"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?
>
> I'm sure this is possible, I just cannot seem to find the magic
> combination that gets me there.  Nothing seems to turn up when I
> look in the Usual Places, but then again, I may not be Lookin' Right.
>
> At the moment, I'm doing things like:
>
> x=raw_input()
> while x:
> Do Something Interesting
> x = get_input("Prompt: ")
>
The approved idiom is

x = raw_input()
while 1:
    if not x:
        break
    DoSomethingInteresting()

However, you may be aware that iterators and generators will shortly make
their appearance in a programming language near you, and this might allow
some other technique.

You might want to look at FAQ 6.30, "Why can't I use an assignment in an
expression" for the rationale.

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list