Guido talked at length about Py3k today at lunch - I took a ton of notes, most of them raw for re-processing to our internal python group at work, but if you're wondering about some of the highlights and information - feel free to read them (warning, they're long). Py3k: Return to Python Mountain makes me very excited to be part of this community at this time and being able to experience (and maybe contribute) to the growth of a language.
Of course, You could also simply read (instead of my notes):
Onto my notes:
- Classes:
- classes() (old)
- class(object) (new)
-
- both will return type "class"
- Print:
- print becomes a function
- print x, y versus print(x, y)
- print x, versus print(x, end=" ")
- print >>f,x versus print(x, file=f)
- Dictionary views
- Inspired by Java Collections Framework
- Remove iterkeys() iteritems() itervalues()
- Change .keys(), .items(), .values()
- These now return a dict view (java map)
- not an iterator
- a lightweight object that can be iterated repeatedly
- .keys(), .items() have set semantics
- .values() has collection semantics
- supports iteration and not much else
- My note: This is much, much cleaner than what we have now.
- Default Comparison Changes
- Default ==, != compare object identity
- Default , >= raises TypeError
- Example: [1,2,""].sort() raises TypeError
- Rationale: 2.x default ordering is bogus
- Unicode String (not implemented)
- Java Like model
- string are always unicode, one str type, unicode
- separate bytes type
- must explicitly specify encoding to go between these
- Open issues:
- implementation
- fixed width chars for O(1) indexing
- maybe 3 internal widths: 1,2,4 byte characters
- C API issues (many C APIS use C char* pointers)
- optimize slicing and concatenation??
- Java Like model
- Bytes type (new)
- mutable sequence o small ints (0-255)
- b[0] is an int, b[:1] is a new bytes object
- implemented efficiently as an unsigned char[]
- has some list-like methods, e.g. extend()
- has some string-like methods, e.g. find()
- but none that depend on locale
- bytes literals: b"ascii or \xDD or 12
- New I/O Library
- stackable component inspired by Java/Perl
- lowest level unbuffered byte I/O
- platform specific, don't use C stdio
- add buffering
- add unicode encoding/decoding
- add CRLF/mapping
- Compatible API
- open(fnam) returns buffered text file
- .read(), .readlines() return strings
- open(name, 'b') returns buffered binary file
- cannot user readlines() here.
- Int/Long unification
- Only one integer type
- it's name is int
- it's implementation is like long in py 2.5
- C API is murky
- Performance could use a boost.
- Int division returns a float now
- Always
- same effect in 2.x with
- from __future__ import division
- use // for int division
- use -Q option to Python 2.x to find old usage
- Raise and Except changes
- all exceptions must derive from BaseException
- exceptions habve __traceback__ attr
- must use raise E(arg) instead of raise E, arg
- can still use raise E and raise without args
- Use raise E(arg).with_traceback(tb)
- instead of raise E, arg, tb
- Use "except E as v:" instead of except E, v:
- Variable v is deleted at the end of the except block!!!
- this is going to get garbage collected/nuked
- Signature Annotations
- Not declarations
- example:
-
- def foo(x: "what", y: list(range(3))) -> 42*2:...
- argument syntax is roughly
- NAME [':' expr] ['=' expr]
- Both expressions are evaluated at def time
- foo.func_annotations is
-
- {'a': 'what', 'b': [0,1,2], "return": 84}
- Self note: You can use these for type checking.
- no other use is made of these annotations
- Keyword-Only parameters
- example def:
-
- def foo(a,b=1,*,c=42,d):...
- example call:
- foo(1,2,d=3)
- cannot use:
- foo(1,2,3) # raises TypeError
- left of the star is positional args, can use keyword if you want
- Set literals
- {1,2,3} is the same as set([1,2,3])
- no empty set literal, use set()
- no frozenset literal, use frozenset()
- Adding set comprehension's
-
- {f(x) for x in S if P(x)}
-
- result is a set
- Absolute Import
- within a package "import foo" does NOT search the package path,
- only sys.path
- use from . import foo for relative import
- or use from import foo
- In 2.4, if you have package P, and have a function named string, it
- will import the local string - not the standard lib. The new
- import . string is a literal "USE MINE" call
- from __future__ import absolute_import in 2.5
- String formatting
- SEE PEP 3101
- "See {0}, {1} and {foo}".format("a", "b", foo="c")
- Shoe Size {0:8}.format(42)
-
- :8 is a type specific piece for formatting, decimal places, empty chars
- Borrowed this from .NET style
- Dict example:
-
- name {0[name]}.format({name:fred})
- Nonlocal Statement
def outer(): x = 42 def inner(): nonlocal x #
- See pep 3104
- Abstract Base Classes
- No pep yet, highly speculative
- introduce a standard abstract class hierarchy for type categories like
- file, container, sequence, etc
- Standard types to use these as base classes to the object(s)
- User defined types MAY USE THESE
- When used, can help distinguish e.g. sequence from mapping or
- file-like behavior or stringiness or numer-icity, etc
- this allows you to "trick" a certain amount of type-iness into
- anything you write (mainly large frameworks)
- Misc Changes:
- exec becomes a function again
- range() becomes xrange()
- input() becomes raw_input()
- zip() returns an iterator
- moved intern() into sys module
- Renamed __nonzero__ o __bool__
- 'as' and 'with' are keywords
- More incoming, already implemented.
- Misc Removals:
- classic classes:new style classes default
- back ticks, use repr()
- removed : use !=
- apply(): use func(*args)
- coerce(), __coerce__: not needed
- dict.has_key gone
- Library Reforms
- Guido not involved in this
- others are interested effort stalled
- need help here
- may happend after 3.0a1
- C API Changes
- too early to tell
- 3 party extensions authors want to know
- For now, simple rules:
-
- Adding APIs is okay
-
- Deleting is OK
-
- Changing APIs incompatibility is NOT okay
- Converting 2.x Code to 3.0
- generic conversion tool exists
- sandbox/2to3
- accuate source-to-source transformation
- parse tree decorated with whitespace and comments
- New conversions are easily added
- create a class from boilerplate
- add a class variable PATTERN to match nodes
- add a method transform() to transform one node
- Separately, Python 2.6 will help
- can warn about out-of-date usages
- not on by default, optionally enabled
- can provide forward-compatible alternatives
- What you can do to prepare
- use 2.6
- derive all exceptions from Exception
- derive all classes from object