Slide 1

Slide 1 text

Python 3.0 where we break all your code Abhinav Sarkar [email protected] [email protected] BARCAMP BANGALORE 7

Slide 2

Slide 2 text

introduction

Slide 3

Slide 3 text

xkcd#365

Slide 4

Slide 4 text

python 3.0 aka Python 3000 aka Py3k aka p3yk

Slide 5

Slide 5 text

where we break all your code

Slide 6

Slide 6 text

backwards incompatible

Slide 7

Slide 7 text

more precisely:

Slide 8

Slide 8 text

almost every program will need changes

Slide 9

Slide 9 text

almost every program will need changes

Slide 10

Slide 10 text

xkcd#349

Slide 11

Slide 11 text

justifications

Slide 12

Slide 12 text

Python is 16 years old

Slide 13

Slide 13 text

b/w compatibility is important

Slide 14

Slide 14 text

python: pre 1.0 lots of changes

Slide 15

Slide 15 text

1.0 -> 2.0 2.0 -> 2.6

Slide 16

Slide 16 text

no b/w incompatible changes

Slide 17

Slide 17 text

none important enough to give presentations on

Slide 18

Slide 18 text

annoying features

Slide 19

Slide 19 text

py3k is the chance to fix

Slide 20

Slide 20 text

mistakes duplications ugly things

Slide 21

Slide 21 text

mistakes coerce() backticks

Slide 22

Slide 22 text

duplication TIOOWTDI not true

Slide 23

Slide 23 text

map list comprehensions

Slide 24

Slide 24 text

string functions string methods

Slide 25

Slide 25 text

apply(foo, args, kwargs) foo(*args, **kwargs)

Slide 26

Slide 26 text

email package rfc822 modules

Slide 27

Slide 27 text

how did this happen?

Slide 28

Slide 28 text

ugly

Slide 29

Slide 29 text

`backticks` repr()

Slide 30

Slide 30 text

reduce() sum() replaces 95% of uses

Slide 31

Slide 31 text

print >> somefile, “stuff”

Slide 32

Slide 32 text

smaller language == better language

Slide 33

Slide 33 text

“python fits in your brain”

Slide 34

Slide 34 text

easier to learn easier to use

Slide 35

Slide 35 text

3.0

Slide 36

Slide 36 text

language changes

Slide 37

Slide 37 text

<> -> !=

Slide 38

Slide 38 text

`backticks` -> repr()

Slide 39

Slide 39 text

removed: print statement

Slide 40

Slide 40 text

!!

Slide 41

Slide 41 text

print() now a function

Slide 42

Slide 42 text

print a, b, c -> print(a, b, c)

Slide 43

Slide 43 text

print >> fp, “stuff” -> print(“stuff”, file=fp)

Slide 44

Slide 44 text

new print feature: customize separators

Slide 45

Slide 45 text

print “,”.join((a,b,c,d)) -> print(a,b,c,d, sep=“,”)

Slide 46

Slide 46 text

new string formatting

Slide 47

Slide 47 text

str.format() “name: {0} {1}”.format(first, last) fmt = “name: {first} {last}” fmt.format(first=“abhinav”, last=“sarkar”)

Slide 48

Slide 48 text

format() builtin >>> format(3.0, “06.1f”) '0003.0'

Slide 49

Slide 49 text

new keywords: as True False None nonlocal

Slide 50

Slide 50 text

basic types

Slide 51

Slide 51 text

strings

Slide 52

Slide 52 text

unicode string default

Slide 53

Slide 53 text

but sometimes you want bytes network protocols binary files

Slide 54

Slide 54 text

bytes() bytes([0x0A, 0x0B, 0x42]) bytes(string, encoding)

Slide 55

Slide 55 text

new ascii() builtin and “%a” string format

Slide 56

Slide 56 text

non-ascii identifiers

Slide 57

Slide 57 text

numbers

Slide 58

Slide 58 text

no more long() ints are long by default no more 123L

Slide 59

Slide 59 text

integer division >>> 1/2 0.5

Slide 60

Slide 60 text

old style >>> 1//2 0

Slide 61

Slide 61 text

dicts

Slide 62

Slide 62 text

dictob.has_key(key) -> key in dictob

Slide 63

Slide 63 text

removed: dict.iter*

Slide 64

Slide 64 text

instead: dictionary views

Slide 65

Slide 65 text

for k in dict: for k,v in dict.items():

Slide 66

Slide 66 text

sets

Slide 67

Slide 67 text

set literals {1,2,3}

Slide 68

Slide 68 text

{} == dict() empty set: set()

Slide 69

Slide 69 text

set comprehensions

Slide 70

Slide 70 text

iterables

Slide 71

Slide 71 text

next() -> __next__()

Slide 72

Slide 72 text

new feature unpacking: first, *rest, last = list

Slide 73

Slide 73 text

head, *rest = somelist *ignore, tail = somelist

Slide 74

Slide 74 text

map(), filter(), zip() -> iterators

Slide 75

Slide 75 text

exceptions

Slide 76

Slide 76 text

no more string exceptions exceptions must derive from BaseException

Slide 77

Slide 77 text

2-arg raise raise MyExcp, val -> raise MyExcp(val)

Slide 78

Slide 78 text

3-arg raise raise MyExcp, val, tback -> raise MyExcp(val).with_traceback(tback)

Slide 79

Slide 79 text

except excp, e -> except excp as e

Slide 80

Slide 80 text

classes

Slide 81

Slide 81 text

new classes only as old-style classes

Slide 82

Slide 82 text

class A(object) -> class A()

Slide 83

Slide 83 text

class decorators @decorator class MyClass: pass

Slide 84

Slide 84 text

more more powerful metaclasses Abstract Base Classes (ABC) abstract methods @abstractproperty

Slide 85

Slide 85 text

functions

Slide 86

Slide 86 text

keyword only args can't be positional

Slide 87

Slide 87 text

buggy example: def fun(arg1, arg2, flag=False): pass fun(1,2,3) # flag = 3 ??

Slide 88

Slide 88 text

the change: keyword args after *args

Slide 89

Slide 89 text

def fun(arg1, arg2, *, flag=False): pass >>> fun(1,2,3) Traceback (most recent call last): File “”, line 1, in TypeError: fun() takes exactly 2 positional arguments (3 given)

Slide 90

Slide 90 text

annotations

Slide 91

Slide 91 text

attaching metadata to arguments

Slide 92

Slide 92 text

syntax: def func(arg: expression)->returnValue: pass

Slide 93

Slide 93 text

expressions can be anything

Slide 94

Slide 94 text

documentation def doEvil(plan: “a plan”) -> “evil results”: .... types def foo(a: float, b: int, c:list) -> dict: .... more complex def processFiles(*files: “one or more of filenames”, delete: “delete when done” = False) -> “a boolean”: ....

Slide 95

Slide 95 text

Python ignore annotations

Slide 96

Slide 96 text

stored in __annotations__

Slide 97

Slide 97 text

3rd party tools: optimizers, docs, IDEs, typechecking, ...

Slide 98

Slide 98 text

files

Slide 99

Slide 99 text

text vs binary text files must have encoding produce unicode binary files produce bytestrings

Slide 100

Slide 100 text

new I/O layer raw I/O buffered I/O text I/O

Slide 101

Slide 101 text

modules

Slide 102

Slide 102 text

absolute imports

Slide 103

Slide 103 text

import foo always imports from the top level

Slide 104

Slide 104 text

import from same package: from . import foo

Slide 105

Slide 105 text

import from parent package: from .. import foo

Slide 106

Slide 106 text

stdlib

Slide 107

Slide 107 text

goals: PEP8 compliance some structure cleaning out cruft

Slide 108

Slide 108 text

PEP 0008 “Modules should have short, all-lowercase names.”

Slide 109

Slide 109 text

BaseHTTPServer cPickle cStringIO HTMLParser ....

Slide 110

Slide 110 text

either renamed, or Flushed.

Slide 111

Slide 111 text

structure

Slide 112

Slide 112 text

current structure

Slide 113

Slide 113 text

duplication and near duplications bsddb gdbm dbm dumbdbm

Slide 114

Slide 114 text

urllib vs urllib2

Slide 115

Slide 115 text

new packages: http html xmlrpc json dbm urllib

Slide 116

Slide 116 text

lots of renames: cStringIO -> io.StringIO SocketServer -> socketserver httplib -> http.client urllib + urllib2 -> urllib

Slide 117

Slide 117 text

cruft

Slide 118

Slide 118 text

lots of purges: old email modules old hash modules old platform modules

Slide 119

Slide 119 text

thread gone use threading

Slide 120

Slide 120 text

UserDict and friends -> subclass from dict

Slide 121

Slide 121 text

porting approach

Slide 122

Slide 122 text

take 2.5 code get working on 2.6 turn -3 flag while True: run through 2to3 run unit tests under 3.0 fix 2.x code

Slide 123

Slide 123 text

2.x -> 2.6 -> 3.0

Slide 124

Slide 124 text

python 2.6 interim release -3 flag: turns on warnings from __future__ from future_builtins import enables some backports

Slide 125

Slide 125 text

2to3 ships with 2.6, 3.0 does mechanical rewrites handles a lot never going to be perfect

Slide 126

Slide 126 text

unit tests

Slide 127

Slide 127 text

eventually ... drop 2.x version switch to 3.0 version

Slide 128

Slide 128 text

things not in py3k

Slide 129

Slide 129 text

antigravity xkcd#353

Slide 130

Slide 130 text

case insensitivity death to lambda implicit self macros

Slide 131

Slide 131 text

not a complete rewrite

Slide 132

Slide 132 text

what happens to python 2.x?

Slide 133

Slide 133 text

No content

Slide 134

Slide 134 text

2.x is not going away will continue to be supported

Slide 135

Slide 135 text

please try 3.0 betas

Slide 136

Slide 136 text

(and report bugs and send fixes)

Slide 137

Slide 137 text

References PEPs http://www.python.org/dev/peps/ http://www.python.org/dev/peps/pep-3100/ Anthony Baxter's OSCON'08 Talk http://www.interlink.com.au/anthony/tech/talks/OSCON2008/

Slide 138

Slide 138 text

exit()