Python 3.
0 Panel
Python CodeLab: Python 3.0
Developer Happiness Days
9th February 2009
Birkbeck College, University of London
Panelists:
● Brian Jinwright
Wofford College
● Benjamin O'Steen
OULS, Oxford University
● Dr Robert Sanderson
Dept of Computer Science, University of Liverpool
● Dr Peter Sefton
Information Services, University of Southern Queensland
Python 3.0 Panel
Topics
● Unicode/String changes
● Print as a Function
● New String Formatting
● Exceptions
● Views and Iterators vs Lists
● Numeric Type Changes
● Dictionary and Set Comprehension
● New io library, Removing file()
● Extended Iterable Unpacking
● Function Argument Annotation
Python 3.0 Panel
Unicode/String Changes
Default string (text) type becomes the 2.x unicode
New Byes type for non text data.
No more u'...' just '...' for text. Also str(b, encoding=codec)
But added b'...' for bytes. Also bytes(s, encoding=codec)
str.encode(codec) --> bytes
bytes.decode(codec) --> str
Cannot mix text and data (raises TypeError)
In 2.x would work if no characters beyond ascii, otherwise UnicodeDecodeError
No more basestring, two separate types.
Python 3.0 Panel
Print as a Function
Old: print “some text here”
New: print(“some text here”)
Old: print >> sys.stderr, “some error”
New: print(“some error”, file=sys.stderr)
Especially important for interactive mode!
Need to retrain fingers to use print() not just print to inspect data
Python 3.0 Panel
New String Formatting
Old: print “something is: %s” % (something)
New: print(“something is: {0}:”.format(something))
Old: print “by name: %(name)” % {'name' : something}
New: print(“by name: {name}”.format(name=something))
New: import sys ; “Python {0.version} on {0.platform}”.format(sys)
New: “a: {0[a]}”.format( {'a' : 'this is a!'} )
Python 3.0 Panel
Exceptions
Old: except KeyError, k:
New: except KeyError as k:
Old: except KeyError, TypeError: # assigns to Type
Old: except (KeyError, TypeError): # catches both
Old: except (KeyError, TypeError), k:
New: except (KeyError, TypeError) as k:
New:
try:
1/0
except Exception as k:
raise MyException from k
Python 3.0 Panel
Views and Iterators vs Lists
Old: k = dict.keys() ; k.sort()
New: k = sorted(dict) # now an (immutable / dynamic ) view
Eg:
d = {1:1, 2:4, 3:9}
k = d.keys() # len(k) == 3
D[4] = 16 # now len(k) == 4
iterkeys(), iteritems(), itervalues() deprecated
map(), filter(), zip() now return iterator not list
Old: range(), xrange()
New: range() works as per xrange, xrange deprecated
Python 3.0 Panel
Numeric Type Changes
Old: int, long
New: int (behaves as long) (no L on the end though!)
Old: 1 / 2 == 0
New: 1 / 2 == 0.5
New: 0o755 (octal)
New: 0b1010 (binary)
Python 3.0 Panel
Dictionary and Set Comprehension
New set literal: {1,2,3,3} == {1,2,3}
{} is still an empty dictionary
Dictionary Comprehension (as per List Comprehension):
{k: v for k, v in stuff}
And Set Comprehension:
{x for x in stuff}
Python 3.0 Panel
New io Library; Removing file()
Old: open(file) or file(file)
New: io.open(file)
open(file) is an alias for this, file(file) is gone
New base stream classes. Eg: IOBase, RawIOBase, BufferedIOBase, TextIOBase,...
sys.stdin, stderr, stdout are instances of io.TextIOBase
BytesIO(data) in place of StringIO()
Python 3.0 Panel
Extended Iterable Unpacking
(a, *rest, b) = range(5)
a = 0, rest = [1,2,3] ; b = 4
Function Argument Annotation
def fn(arg1 : something, arg2 : something2) -> returnsSomething
All totally optional, no semantics for annotations
Eg could be string with description or class/type for type checking or ...
Accessible via func.__annotations__ dictionary
def foo(...): ...
print(foo.__annotations__)