I've built Python 3.0 RC1 (First Release Candidate) to test out what it's going to do to our projects. This blog is specifically dedicated to a list of the changes that affect us. I will update it as I find more things (I'm going to literally go through all my projects and all the class code examples and say what doesn't work.)
If you have any questions about your particular projects / code snippets, you can send them to me at amentajo@msu.edu or simply post as a comment here. PLEASE don't send me the current working project or any significant portions of it. (Project 10 now...)
I'm willing to put my time into testing anything sent to me, so don't hesitate to send me things to test!
Python 3.0 RC1 incompatibilities:
--strings
----String formatting with the % operator is done away with, and replaced by a format() method for strings. 'Hello, %s' % ('world') has turned into 'Hello, {0}'.format('world'). In general, it looks like 'Sometext {n}'.format('a','b','c','d') where the {n} refers to the index of the tuple passed to format(). See the official readme for more details on this matter. It has been confirmed that the previous statement is false. In 3.0, print('Hello %s' % ('world')) works, as do all other implementations of the % formatting character we have learned in class. (See comment below about print()). My previous confusion was due to the fact that print is a function now.
--integers
----integer division returns a float... 1/2 => 0.5, 1//2 => 0
----long integers are now just integers, and there is no max length
--print:
----print no longer works 'without parentheses'. You must use print("Hello World"). print "Hello World" will no longer work. This is because print has been changed from a statement into a function: print(x) rather than print x.
--raw_input:
----raw_input() has been replaced with input(), which appears to work exactly the same.
--range:
----range() now returns an iterable instead of a list. That is to say, x = range(1,10) will return range(1,10) when x is called, but for x in range(1,10): print (x, end = '') will print 123456789. (This is the behaviour of a function that exists in 2.5 called xrange(), which no longer exists in 3.0)
Specific tests of Class Code Examples (python 3.0 and 2.6 come with a script called '2to3' that refactors certain lines of 2.x code to work with 3.0. I tested the examples after running 2to3 on them, and these are the results):
01-04:
fixed with 2to3...
05-haiku:
not fixed with 2to3... error at line 6, "NameError: name 'file' is not defined" This appears to be because in 3.0, various builtins were removed, including file(). Not sure what a workaround may be.
06-10:
fixed with 2to3...
11-password:
almost fixed with 2to3... change the variable name input to something else, as input() is now a function.
11.5-Palindromes:
not fixed with 2to3... multiple functions do not operate properly.
12-simpleLists:
fixed with 2to3...
13-forExample:
fixed with 2to3 and changing range(1-11) to range(1,11)
14-perfectNumbers:
fixed with 2to3, but be careful - the commented lines of print statements will NOT run if uncommented after 2to3. Please make all changes to original 2.x modules before running 2to3, and rerun it every time you make a change.
15-16B:
fixed with 2to3...
16C-mileagePuz:
fixed with 2to3... note that the time output for 3.0 appears to be approximately 25% faster than the same module in 2.6. (running Ubuntu Linux 8.04, Hardy Heron (linux kernel 2.6.24-21-generic), Intel Core 2 Duo (1.00GHz * 2), the averages of the two are 300ms in 3.0, 400ms in 2.6.)
17-19:
fixed with 2to3...
20-sets:
simpleSets.py is broken in 2.5 and 2.6: lists are unhashable. 2to3 keeps this error.
21-dicts:
fixed with 2to3...
21.5-funcParams:
not fixed with 2to3, but very fixable. change commas in print functions to %( and close parentheses at the end.
22-classes:
fixed with 2to3... previously reported as broken, but works as intended...
23-scope:
fixed with 2to3...
24-note:
not fixed with 2to3... must delete the s.print call, or define the method (print is now a function, not a statement)
25-27:
fixed with 2to3...
28-expansion:
almost fixed with 2to3... line 35 is malindented. Add another space there. Also, make sure example 27's Rational.py is in PYTHONPATH or you will get an ImportError.
29,30:
fixed with 2to3
31-PythonArray:
not fixed with 2to3... NumPy is not 3.x ready.
32-exceptionExamples:
not all fixed with 2to3...
fullException.py:
types that were once "file" are now class objects io.TextIOWrapper. Fix by importing the io module then changing line 23 to:
if type(fs)== io.TextIOWrapper:
exceptionFlow.py:
fixed with 2to3...
raise.py:
working, but be careful of the name conflict with 'raise'. No importing raise or anything from raise, but you can run it otherwise.
33-sort_and_search:
sort works with 2to3, but search needs one adjustment: since int division returns a float, line 26 needs to be converted to an int with the int() function after refactoring.
34-scriptExample:
fixed with 2to3...
35-_testing:
doctestExample.py: problems with long integers: long() is now named int() and L suffixes are removed in 3.0.
unittestExample.py: fixed with 2to3...
36-_PythonToC++:
irrelevant; the dateClass.py module is in example 22.
amentajo@msu.edu
Subscribe to:
Post Comments (Atom)

2 comments:
How does print() handle the ending newline? Does it still automatically insert one, and if so, would it be supressed as:
print(format_string, % (format));
(Where normal Python would omit the comma.)
No. The proper way to do it appears to be print(format_string % (format));
The comma can be used in this way:
print('An','unlimited','number','of','things','can','go','here.',sep=' ',end='\n') would print this in Python:
'An unlimited number of things can go here.\n'
Another example: (default sep==' ', default end=='\n')
print('This','one','time','at','band','camp',sep=', like, ',end=', like, I, um, totally!\n') prints:
'This, like, one, like, time, like, at, like, band, like, camp, like, I, um, totally!\n'
(As always, replace '\n' with a new line in reading it.)
Post a Comment