Can python find fibonacci series in a single line of code?

Duncan Booth duncan at NOSPAMrcp.co.uk
Tue Nov 12 09:12:09 EST 2002


bokr at oz.net (Bengt Richter) wrote in news:aqqt9t$vu2$0 at 216.39.172.122:

>>  python -c "a=b=1;exec'while+1:print+a;a,b=b,a+b'"
<snip>
> 
> I didn't know exec would parse semicolons that way (I hardly ever use
> it). But is that new with 2.2? Does that mean it effectively assumes a
> semicolon is the same as a newline and indent to the same level as the
> previous statement, until it finds an actual newline?

No, it simply means that:
   while+1:print+a;a,b=b,a+b
is a legal line, with or without exec.

The while has to be the first statement on the line thoug, which is which 
you need the exec here (or tricks updating vars()).

How about this variant? No sneaky updating vars(), no exec:

  while 1: g=vars().get;a,b=g('a',1),g('b',1);print a;a,b=b,a+b


-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list