Python 3000 (PyCon, 24-Feb-02007)‏ Guido van Rossum [email_address] [email_address]
What Is Python 3000? <ul><li>The next  major  Python release </li></ul><ul><ul><li>To be released as Python 3.0 </li></ul>...
Activity Since Last Year <ul><li>Lots of design discussions </li></ul><ul><ul><li>(too many, if you ask me :-)‏ </li></ul>...
Python 3.0 Timeline <ul><li>PEPs to be completed: April 2007 </li></ul><ul><li>3.0a1: June 2007 </li></ul><ul><li>3.0 fina...
Rest of the Talk <ul><li>Highlight some of the most visible changes </li></ul><ul><ul><li>print function, dict views, comp...
No More Classic Classes <ul><li>In 2.2 … 2.9: </li></ul><ul><ul><li>class C: # classic class (0.1 … 2.1)‏ </li></ul></ul><...
Print is a Function <ul><li>print x, y -> print(x, y)‏ </li></ul><ul><li>print x, -> print(x, end=&quot; &quot;)‏ </li></u...
Dictionary Views <ul><li>Inspired by Java Collections Framework </li></ul><ul><li>Remove .iterkeys(), .iteritems(), .iterv...
Default Comparison Changed <ul><li>Default ==, != compare object identity </li></ul><ul><ul><li>(this is unchanged)‏ </li>...
**Unicode Strings <ul><li>Java-like model: </li></ul><ul><ul><li>strings (the str type) are always Unicode </li></ul></ul>...
The Bytes Type <ul><li>A  mutable  sequence of small ints (0…255)‏ </li></ul><ul><ul><li>b[0] is an int; b[:1] is a new by...
**New I/O Library <ul><li>Stackable components (inspired by Java, Perl)‏ </li></ul><ul><ul><li>Lowest level: unbuffered by...
Int/Long Unification <ul><li>There is only one built-in integer type </li></ul><ul><li>Its name is int </li></ul><ul><li>I...
Int Division Returns a Float <ul><li>Always! </li></ul><ul><li>Same effect in 2.x with </li></ul><ul><ul><li>from __future...
**Raise and Except Changes <ul><li>All exceptions must derive from BaseException </li></ul><ul><li>Exceptions have __trace...
Signature Annotations <ul><li>NOT  type declarations! </li></ul><ul><li>Example: </li></ul><ul><ul><li>def foo(x: &quot;wh...
Keyword-Only Parameters <ul><li>Example def: </li></ul><ul><ul><li>def foo(a, b=1, *, c=42, d): … </li></ul></ul><ul><li>E...
Set Literals <ul><li>{1, 2, 3} is the same as set([1, 2, 3])‏ </li></ul><ul><li>No empty set literal; use set()‏ </li></ul...
Absolute Import <ul><li>Same effect in 2.5 with </li></ul><ul><ul><li>from __future__ import absolute_import </li></ul></u...
**String Formatting <ul><li>Examples (see PEP 3101 for more): </li></ul><ul><ul><li>&quot;See {0}, {1} and {foo}&quot;.for...
**Nonlocal Statement <ul><li>def outer():   x = 42   def inner():   nonlocal x # <---- new   print(x)   x += 1   return in...
**Abstract Base Classes? <ul><li>Still highly speculative (no PEP yet)‏ </li></ul><ul><ul><li>wiki.python.org/moin/Abstrac...
**Switch/Case Statement??? <ul><li>Highly speculative; see PEP 3103 </li></ul><ul><ul><li>switch EXPR:   case EXPR:   SUIT...
Miscellaneous Changes <ul><li>exec becomes a function again </li></ul><ul><li>range() becomes xrange()‏ </li></ul><ul><li>...
Miscellaneous Removals <ul><li>classic classes: new-style classes default </li></ul><ul><li>backticks: use repr()‏ </li></...
**Library Reform <ul><li>Not my priority </li></ul><ul><li>Others are interested, but effort seems stalled </li></ul><ul><...
*C API Changes <ul><li>Too early to tell what will happen </li></ul><ul><li>3rd party extension authors want to know </li>...
Converting 2.x Code to 3.0 <ul><li>Generic conversion tool exists </li></ul><ul><ul><li>sandbox/2to3 </li></ul></ul><ul><u...
Examples of What It Can Do <ul><li>apply(fun, args, kwds) -> fun(*args, **kwds)‏ </li></ul><ul><li>d.iterkeys() -> d.keys(...
Examples of What It  Can't  Do <ul><li>detect whether d is a dict (in d.iterkeys())‏ </li></ul><ul><li>detect whether you ...
What You Can Do Today <ul><li>Don't worry about stuff that can be automated </li></ul><ul><li>Don't try to write source-le...
Questions
Upcoming SlideShare
Loading in …5
×

Python 3000

6,999 views

Published on

Guido Van Rossum presentation on the next change of Python 3000

Published in: Technology
3 Comments
17 Likes
Statistics
Notes
No Downloads
Views
Total views
6,999
On SlideShare
0
From Embeds
0
Number of Embeds
129
Actions
Shares
0
Downloads
0
Comments
3
Likes
17
Embeds 0
No embeds

No notes for slide

Python 3000

  1. 1. Python 3000 (PyCon, 24-Feb-02007)‏ Guido van Rossum [email_address] [email_address]
  2. 2. What Is Python 3000? <ul><li>The next major Python release </li></ul><ul><ul><li>To be released as Python 3.0 </li></ul></ul><ul><li>The first one in a long time to be incompatible </li></ul><ul><ul><li>But not completely different or unusual </li></ul></ul><ul><li>Concept first formed around 2000 </li></ul><ul><ul><li>Py3k nickname was a play on Windows 2000 </li></ul></ul><ul><li>Goal: to correct my early design mistakes </li></ul><ul><ul><li>Those that would require incompatibility to fix </li></ul></ul><ul><ul><li>Reduce cognitive load for first-time learners </li></ul></ul><ul><li>Work and thinking started for real last year </li></ul>
  3. 3. Activity Since Last Year <ul><li>Lots of design discussions </li></ul><ul><ul><li>(too many, if you ask me :-)‏ </li></ul></ul><ul><li>Some PEPs were written </li></ul><ul><ul><li>(but not enough…)‏ </li></ul></ul><ul><li>Lots of code was written </li></ul><ul><ul><li>(just the right amount!)‏ </li></ul></ul><ul><ul><li>(but we're not done yet!!)‏ </li></ul></ul>
  4. 4. Python 3.0 Timeline <ul><li>PEPs to be completed: April 2007 </li></ul><ul><li>3.0a1: June 2007 </li></ul><ul><li>3.0 final: June 2008 </li></ul><ul><li>For comparison, the 2.6 timeline: </li></ul><ul><li>2.6a1: December 2007 </li></ul><ul><li>2.6 final: April 2008 </li></ul><ul><li>There will also be a 2.7 timeline </li></ul>
  5. 5. Rest of the Talk <ul><li>Highlight some of the most visible changes </li></ul><ul><ul><li>print function, dict views, comparisons, unicode, … </li></ul></ul><ul><li>How to convert 2.x to 3.0 code </li></ul><ul><li>Notational convention: </li></ul><ul><ul><li>* = incompletely implemented </li></ul></ul><ul><ul><li>** = not yet implemented </li></ul></ul>
  6. 6. No More Classic Classes <ul><li>In 2.2 … 2.9: </li></ul><ul><ul><li>class C: # classic class (0.1 … 2.1)‏ </li></ul></ul><ul><ul><li>class C(object): # new-style class (old now :-)‏ </li></ul></ul><ul><li>In 3.0: </li></ul><ul><ul><li>both are new-style classes (just say &quot;classes&quot;)‏ </li></ul></ul><ul><li>Differences are subtle, few of you will notice </li></ul>
  7. 7. Print is a Function <ul><li>print x, y -> print(x, y)‏ </li></ul><ul><li>print x, -> print(x, end=&quot; &quot;)‏ </li></ul><ul><li>print >>f, x -> print(x, file=f)‏ </li></ul><ul><li>Automatic translation is 98% correct </li></ul><ul><li>Fails for cases involving softspace cleverness: </li></ul><ul><ul><li>print &quot;x &quot;, &quot;y&quot; doesn 't insert a space before y </li></ul></ul><ul><ul><li>print(&quot;x &quot;, &quot;y&quot;) does </li></ul></ul><ul><ul><li>ditto for print &quot;x &quot;, &quot;y&quot; </li></ul></ul>
  8. 8. Dictionary Views <ul><li>Inspired by Java Collections Framework </li></ul><ul><li>Remove .iterkeys(), .iteritems(), .itervalues()‏ </li></ul><ul><li>Change .keys(), .items(), .values()‏ </li></ul><ul><li>These return a dict view </li></ul><ul><ul><li>Not an iterator </li></ul></ul><ul><ul><li>A lightweight object that can be iterated repeatedly </li></ul></ul><ul><ul><li>.keys(), .items() have set semantics </li></ul></ul><ul><ul><li>.values() has &quot;collection&quot; semantics </li></ul></ul><ul><ul><ul><li>supports iteration and not much else </li></ul></ul></ul>
  9. 9. Default Comparison Changed <ul><li>Default ==, != compare object identity </li></ul><ul><ul><li>(this is unchanged)‏ </li></ul></ul><ul><li>Default <, <=, >, >= raise TypeError </li></ul><ul><li>Example: [1, 2, &quot;&quot;].sort() raises TypeError </li></ul><ul><li>Rationale: 2.x default ordering is bogus </li></ul><ul><ul><li>depends on type names </li></ul></ul><ul><ul><li>depends on addresses </li></ul></ul>
  10. 10. **Unicode Strings <ul><li>Java-like model: </li></ul><ul><ul><li>strings (the str type) are always Unicode </li></ul></ul><ul><ul><li>separate bytes type </li></ul></ul><ul><ul><li>must explicitly specify encoding to go between these </li></ul></ul><ul><li>Open issues: </li></ul><ul><ul><li>implementation </li></ul></ul><ul><ul><ul><li>fixed-width characters for O(1) indexing </li></ul></ul></ul><ul><ul><ul><li>maybe 3 internal widths: 1, 2, 4 byte characters </li></ul></ul></ul><ul><ul><ul><li>C API issues (many C APIs use C char* pointers)‏ </li></ul></ul></ul><ul><ul><li>optimize slicing and concatenation??? </li></ul></ul><ul><ul><ul><li>lots of issues, supporters, detractors </li></ul></ul></ul>
  11. 11. The Bytes Type <ul><li>A mutable sequence of small ints (0…255)‏ </li></ul><ul><ul><li>b[0] is an int; b[:1] is a new bytes object </li></ul></ul><ul><li>Implemented efficiently as unsigned char[] </li></ul><ul><li>Has some list-like methods, e.g. .extend()‏ </li></ul><ul><li>Has some string-like methods, e.g. .find()‏ </li></ul><ul><ul><li>But none that depend on locale </li></ul></ul><ul><li>bytes literals: b&quot;ascii or xDD or 12&quot; </li></ul><ul><li>bytes has .decode() method returning a string </li></ul><ul><li>str has a .encode() method returning bytes </li></ul>
  12. 12. **New I/O Library <ul><li>Stackable components (inspired by Java, Perl)‏ </li></ul><ul><ul><li>Lowest level: unbuffered byte I/O </li></ul></ul><ul><ul><ul><li>platform-specific; don't use C stdio </li></ul></ul></ul><ul><ul><li>Add buffering </li></ul></ul><ul><ul><li>Add unicode encoding/decoding </li></ul></ul><ul><ul><ul><li>encoding explicitly specified or somehow guessed </li></ul></ul></ul><ul><ul><li>Add CRLF/LF mapping </li></ul></ul><ul><li>Compatible API </li></ul><ul><ul><li>open(filename) returns a buffered text file </li></ul></ul><ul><ul><ul><li>read() and readline() return strings </li></ul></ul></ul><ul><ul><li>open(filename, &quot;b&quot;) returns a buffered binary file </li></ul></ul><ul><ul><ul><li>read() returns bytes; can't use readline()‏ </li></ul></ul></ul>
  13. 13. Int/Long Unification <ul><li>There is only one built-in integer type </li></ul><ul><li>Its name is int </li></ul><ul><li>Its implementation is like long in Python 2.x </li></ul><ul><li>C API is a bit murky </li></ul><ul><li>Performance could use a boost </li></ul>
  14. 14. Int Division Returns a Float <ul><li>Always! </li></ul><ul><li>Same effect in 2.x with </li></ul><ul><ul><li>from __future__ import division </li></ul></ul><ul><li>Use // for int division </li></ul><ul><li>Use -Q option to Python 2.x to find old usage </li></ul>
  15. 15. **Raise and Except Changes <ul><li>All exceptions must derive from BaseException </li></ul><ul><li>Exceptions have __traceback__ attribute </li></ul><ul><li>Must use raise E(arg) instead of raise E, arg </li></ul><ul><li>Can still use raise E and raise without args </li></ul><ul><li>Use raise E(arg).with_traceback(tb)‏ </li></ul><ul><ul><li>instead of raise E, arg, tb </li></ul></ul><ul><li>Use &quot;except E as v:&quot; instead of &quot;except E, v:&quot; </li></ul><ul><li>Variable v is deleted at end of except block!!! </li></ul>
  16. 16. Signature Annotations <ul><li>NOT type declarations! </li></ul><ul><li>Example: </li></ul><ul><ul><li>def foo(x: &quot;whatever&quot;, y: list(range(3))) -> 42*2: … </li></ul></ul><ul><li>Argument syntax is (roughly): </li></ul><ul><ul><li>NAME [':' expr] ['=' expr] </li></ul></ul><ul><li>Both expressions are evaluated at 'def' time </li></ul><ul><ul><li>foo.func_annotations is: </li></ul></ul><ul><ul><ul><li>{'a': &quot;whatever&quot;, 'b': [0, 1, 2], &quot;return&quot;: 84} </li></ul></ul></ul><ul><ul><li>NO other use is made of these annotations </li></ul></ul>
  17. 17. Keyword-Only Parameters <ul><li>Example def: </li></ul><ul><ul><li>def foo(a, b=1, *, c=42, d): … </li></ul></ul><ul><li>Example call: </li></ul><ul><ul><li>foo(1, 2, d=3)‏ </li></ul></ul><ul><li>Cannot use: </li></ul><ul><ul><li>foo(1, 2, 3) # raises TypeError </li></ul></ul>
  18. 18. Set Literals <ul><li>{1, 2, 3} is the same as set([1, 2, 3])‏ </li></ul><ul><li>No empty set literal; use set()‏ </li></ul><ul><li>No frozenset literal; use frozenset({…})‏ </li></ul><ul><li>**Set comprehensions: </li></ul><ul><ul><li>{ f ( x ) for x in S if P ( x )} </li></ul></ul><ul><ul><ul><li>same as set( f ( x ) for x in S if P ( x ))‏ </li></ul></ul></ul>
  19. 19. Absolute Import <ul><li>Same effect in 2.5 with </li></ul><ul><ul><li>from __future__ import absolute_import </li></ul></ul><ul><li>Within a package &quot;import foo&quot; does NOT search the package path, only sys.path </li></ul><ul><li>Use &quot;from . import foo&quot; for relative import </li></ul><ul><li>Or use from <full-package-name> import foo </li></ul>
  20. 20. **String Formatting <ul><li>Examples (see PEP 3101 for more): </li></ul><ul><ul><li>&quot;See {0}, {1} and {foo}&quot;.format(&quot;A&quot;, &quot;B&quot;, foo=&quot;C&quot;)‏ </li></ul></ul><ul><ul><ul><li>&quot;See A, B and C&quot; </li></ul></ul></ul><ul><ul><li>&quot;my name is {0} :-{{}}&quot;.format(&quot;Fred&quot;)‏ </li></ul></ul><ul><ul><ul><li>&quot;my name is Fred :-{}&quot; </li></ul></ul></ul><ul><ul><li>&quot;File name {0.foo}&quot;.format(open(&quot;foo.txt&quot;))‏ </li></ul></ul><ul><ul><ul><li>File name foo.txt </li></ul></ul></ul><ul><ul><li>&quot;Name is {0[name]}&quot;.format({&quot;name&quot;: &quot;Fred&quot;})‏ </li></ul></ul><ul><ul><ul><li>&quot;Name is Fred&quot; </li></ul></ul></ul><ul><ul><li>Shoe size {0:8}&quot;.format(42)‏ </li></ul></ul><ul><ul><ul><li>&quot;Shoe size 42&quot; </li></ul></ul></ul>
  21. 21. **Nonlocal Statement <ul><li>def outer(): x = 42 def inner(): nonlocal x # <---- new print(x) x += 1 return inner </li></ul><ul><li>Doesn't work today; x becomes a local in inner </li></ul><ul><li>Different keywords proposed: </li></ul><ul><ul><li>nonlocal, global, outer, … (see PEP 3104)‏ </li></ul></ul>
  22. 22. **Abstract Base Classes? <ul><li>Still highly speculative (no PEP yet)‏ </li></ul><ul><ul><li>wiki.python.org/moin/AbstractBaseClasses </li></ul></ul><ul><li>Introduce a standard abstract class hierarchy for type categories like file, container, sequence, iterable etc. </li></ul><ul><li>Standard types to use these as base classes </li></ul><ul><li>User-defined types may use these </li></ul><ul><li>When used, can help distinguishing e.g. sequence from mapping, or file-like behavior, or &quot;stringiness&quot;, or &quot;numericity&quot;, etc. </li></ul>
  23. 23. **Switch/Case Statement??? <ul><li>Highly speculative; see PEP 3103 </li></ul><ul><ul><li>switch EXPR: case EXPR: SUITE case EXPR: # or case in EXPRLIST: SUITE … [else: SUITE] </li></ul></ul><ul><li>Problem: when to compile EXPR? </li></ul><ul><ul><li>Would prefer precompilation for faster execution </li></ul></ul><ul><ul><li>But this would introduce unusual semantics </li></ul></ul>
  24. 24. Miscellaneous Changes <ul><li>exec becomes a function again </li></ul><ul><li>range() becomes xrange()‏ </li></ul><ul><li>input() becomes raw_input()‏ </li></ul><ul><li>zip() returns an iterator </li></ul><ul><li>Moved intern() into sys module </li></ul><ul><li>Renamed __nonzero__ to __bool__ </li></ul><ul><li>'as' and 'with' are keywords </li></ul><ul><li>And more, planned and implemented </li></ul>
  25. 25. Miscellaneous Removals <ul><li>classic classes: new-style classes default </li></ul><ul><li>backticks: use repr()‏ </li></ul><ul><li>Removed <>: use != </li></ul><ul><li>apply(): use func(*args)‏ </li></ul><ul><li>coerce(), __coerce__: not needed </li></ul><ul><li>dict.has_key(): use key in dict </li></ul><ul><li>'softspace' attribute on file objects </li></ul>
  26. 26. **Library Reform <ul><li>Not my priority </li></ul><ul><li>Others are interested, but effort seems stalled </li></ul><ul><li>Need help! </li></ul><ul><li>May happen after 3.0a1 is released </li></ul>
  27. 27. *C API Changes <ul><li>Too early to tell what will happen </li></ul><ul><li>3rd party extension authors want to know </li></ul><ul><li>For now, these simple rules: </li></ul><ul><ul><li>Adding APIs is okay (of course)‏ </li></ul></ul><ul><ul><li>Deleting APIs is okay </li></ul></ul><ul><ul><li>Changing APIs incompatibly is NOT OKAY </li></ul></ul>
  28. 28. Converting 2.x Code to 3.0 <ul><li>Generic conversion tool exists </li></ul><ul><ul><li>sandbox/2to3 </li></ul></ul><ul><ul><li>accurate source-to-source transformation </li></ul></ul><ul><ul><li>parse tree decorated with whitespace & comments </li></ul></ul><ul><li>New conversions are easily added </li></ul><ul><ul><li>create a class from boilerplate </li></ul></ul><ul><ul><li>add a class variable PATTERN to match nodes </li></ul></ul><ul><ul><li>add a method transform() to transform one node </li></ul></ul><ul><li>Separately, Python 2.6 will help </li></ul><ul><ul><li>can warn about out-of-date usages </li></ul></ul><ul><ul><li>can provide forward-compatible alternatives </li></ul></ul>
  29. 29. Examples of What It Can Do <ul><li>apply(fun, args, kwds) -> fun(*args, **kwds)‏ </li></ul><ul><li>d.iterkeys() -> d.keys()‏ </li></ul><ul><li>exec a in b, c -> exec(a, b, c)‏ </li></ul><ul><li>print >>sys.stderr, x, -> print(x, end=&quot; &quot;, file=sys.stderr)‏ </li></ul><ul><li>except E, v: -> except E as v: </li></ul><ul><li>d.has_key(k) -> k in d </li></ul><ul><li>intern(s) -> sys.intern(s)‏ </li></ul><ul><li>a <> b -> a != b; `x` -> repr(x); int -> long </li></ul><ul><li>automatically adds parentheses where needed </li></ul>
  30. 30. Examples of What It Can't Do <ul><li>detect whether d is a dict (in d.iterkeys())‏ </li></ul><ul><li>detect whether you use d.keys() as a list later </li></ul><ul><li>turn int()/int() into int()//int()‏ </li></ul><ul><li>fix code that depends on int() < str()‏ </li></ul><ul><li>remove redundant code </li></ul><ul><li>fix custom classes emulating dictionaries </li></ul><ul><li>fix string exceptions, non-Exception exceptions </li></ul><ul><li>in general: limited to syntactic conversions </li></ul><ul><ul><li>can't follow control flow, doesn't do type inference </li></ul></ul>
  31. 31. What You Can Do Today <ul><li>Don't worry about stuff that can be automated </li></ul><ul><li>Don't try to write source-level compatible code </li></ul><ul><ul><li>Use Python 2.6 when it comes out </li></ul></ul><ul><ul><li>Write unit tests with maximal coverage </li></ul></ul><ul><ul><li>Use keys = sorted(d.iterkeys())‏ </li></ul></ul><ul><ul><li>Use list(d.iterkeys()) when you really need a list </li></ul></ul><ul><ul><li>Derive all exceptions from Exception </li></ul></ul><ul><ul><li>Derive all classes from object </li></ul></ul><ul><ul><li>Don't rely on subtle print/softspace semantics </li></ul></ul><ul><ul><ul><li>use print line.rstrip(&quot; &quot;) instead of print line, </li></ul></ul></ul><ul><ul><li>Use // for int division </li></ul></ul>
  32. 32. Questions

×