fluentpython / example-code

Example code for the book Fluent Python, 1st Edition (O'Reilly, 2015)
http://bit.ly/fluentpy
MIT License
5.56k stars 2.18k forks source link

listcomp_speed.py will rasise a error in python2.7 and run well in python3.5 #10

Closed wllbll closed 5 years ago

wllbll commented 7 years ago

the code in listcomp_speed.py will rasise an syntax error: def clock(label, cmd): ... res = time.repeat(cmd, setup=SETUP, number=TIMES) ... print(label, ('{:3.f}.formart(x) for x in res')) File "", line 3 print(label, ('{:3.f}.formart(x) for x in res')) ^ SyntaxError: invalid syntax

but it doesn't make sense to me, as the asterisk in print is unpacking, and unpacking is support in print, as print(*("1","2")) will work well.

and the same code run well in python3.5.

the book did not specify the environment of python.

ramalho commented 5 years ago

Hello, @wllbll . This is what I wrote in the Preface (section Who This Book is For): """ However, Fluent Python is about making the most of Python 3.4, and I do not spell out the fixes needed to make the code work in earlier versions. Most examples should run in Python 2.7 with little or no changes, but in some cases, backporting would require significant rewriting. """

It was never my stated goal to cover both Python 2.7 and 3.4. My focus was always Python 3.4.