
A couple of days ago, I mentioned Python 3 in general terms - and today I'm starting to flesh out the road ahead.
print
The
print operator in Python was always a bit curious, with the trailing comma to signify "no new line" ... and that has been replaced by a
print function, with named parameters for the end of line action, parameter separator, and output file - it's much more flexible in it sown right, and it allows you to override the printg function which you could not do with an operator. Here are some sample print function calls:
print (teeth,"teeth and",coal,"shoes")
print (teeth,"teeth and",coal,"shoes",sep = " - ")
print (teeth,"teeth and",end = "")
print (coal,"shoes")
print ("Error and logging message",file = sys.stderr)
See
complete Python 3 example and
Python 2 equivalent
input and raw_input
The
input function, which used to process what the user typed through the Python interpreter has gone (thank goodness!) and the old
raw_input functionality, that did what we wanted 99.9% of the time for input has now simply been named
input. This will be short term confusing, but a big relief in the medium and long term.
See
complete Python 3 example and
Python 2 equivalent
range and xrange
The original
range function - many moons ago - returned a list, which you could then step through in a for loop. Which was great for a small list and really grotty for a longer one - rather like blowing up a balloon (generating a list) and then letting the air out of it slowly. And of course, if you blow a balloon up too much it bursts! The
xrange function was added to provide an
iterator - rather than blowing up a balloon, the information is generated just-in-time, very much like a
generator
Moving on to Python 3,
range is now a generator (and
xrange has gone), which means that it will default to being tidy.
The same applies to things like map as well, and you will need to add a list function call there if you don't want to iterate through a mapping
Here's a generator in use (in Python 3) with the
range function itself producing an iterator:
def squares(g):
for val in range(g):
print ("working out ",val)
yield val*val
for ref in squares(8):
print ("Board with ",ref," squares")
and here is what that code would look like on Python 2:
def squares(g):
for val in xrange(g):
print "working out ",val
yield val*val
for ref in squares(8):
print "Board with ",ref," squares"
Our next
public Python Course will include a
module on Python 3 changes, covering not only the new features, but also the way to make best use of them and to plan for them and easily convert from legacy Python code - moving Python forward on a practical basis. In due course (estimate - 3 to 6 months, depending on market requirements) the whole course will be Python 3 based.
We can also provide a one day seminar for current python users, introducing them to the new facilities and changes and helping them plan a painless transition for the way forward. Please
email me for details.
(written 2008-09-10 07:53:43)
Associated topics are indexed under
Y300 - Python 3 - What is New?
Some other Articles
What have iTime, honeytrapagency and domain listing center got in common?Refactoring - a PHP demo becomes a production pageWhich country does a search engine think you are located in?All the pieces fall into place - hotel and coursesThe road ahead - Python 3Sharing variables with functions, but keeping them local too - PythonLooking for a value in a list - PythonPython 2 to Python 3 / Python 3000 / Py3kHowto - write and manage a news box on your web pagePicturing the rain