Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Python 3.0: Where we break all your code
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Abhinav Sarkar
September 13, 2008
Programming
120
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Python 3.0: Where we break all your code
Abhinav Sarkar
September 13, 2008
Transcript
138 slides
Python 3.0 where we break all your code Abhinav Sarkar
[email protected]
[email protected]
BARCAMP BANGALORE 7
introduction
xkcd#365
python 3.0 aka Python 3000 aka Py3k aka p3yk
where we break all your code
backwards incompatible
more precisely:
almost every program will need changes
almost every program will need changes
xkcd#349
justifications
Python is 16 years old
b/w compatibility is important
python: pre 1.0 lots of changes
1.0 -> 2.0 2.0 -> 2.6
no b/w incompatible changes
none important enough to give presentations on
annoying features
py3k is the chance to fix
mistakes duplications ugly things
mistakes coerce() backticks
duplication TIOOWTDI not true
map list comprehensions
string functions string methods
apply(foo, args, kwargs) foo(*args, **kwargs)
email package rfc822 modules
how did this happen?
ugly
`backticks` repr()
reduce() sum() replaces 95% of uses
print >> somefile, “stuff”
smaller language == better language
“python fits in your brain”
easier to learn easier to use
3.0
language changes
<> -> !=
`backticks` -> repr()
removed: print statement
!!
print() now a function
print a, b, c -> print(a, b, c)
print >> fp, “stuff” -> print(“stuff”, file=fp)
new print feature: customize separators
print “,”.join((a,b,c,d)) -> print(a,b,c,d, sep=“,”)
new string formatting
str.format() “name: {0} {1}”.format(first, last) fmt = “name: {first} {last}”
fmt.format(first=“abhinav”, last=“sarkar”)
format() builtin >>> format(3.0, “06.1f”) '0003.0'
new keywords: as True False None nonlocal
basic types
strings
unicode string default
but sometimes you want bytes network protocols binary files
bytes() bytes([0x0A, 0x0B, 0x42]) bytes(string, encoding)
new ascii() builtin and “%a” string format
non-ascii identifiers
numbers
no more long() ints are long by default no more
123L
integer division >>> 1/2 0.5
old style >>> 1//2 0
dicts
dictob.has_key(key) -> key in dictob
removed: dict.iter*
instead: dictionary views
for k in dict: for k,v in dict.items():
sets
set literals {1,2,3}
{} == dict() empty set: set()
set comprehensions
iterables
next() -> __next__()
new feature unpacking: first, *rest, last = list
head, *rest = somelist *ignore, tail = somelist
map(), filter(), zip() -> iterators
exceptions
no more string exceptions exceptions must derive from BaseException
2-arg raise raise MyExcp, val -> raise MyExcp(val)
3-arg raise raise MyExcp, val, tback -> raise MyExcp(val).with_traceback(tback)
except excp, e -> except excp as e
classes
new classes only as old-style classes
class A(object) -> class A()
class decorators @decorator class MyClass: pass
more more powerful metaclasses Abstract Base Classes (ABC) abstract methods
@abstractproperty
functions
keyword only args can't be positional
buggy example: def fun(arg1, arg2, flag=False): pass fun(1,2,3) # flag
= 3 ??
the change: keyword args after *args
def fun(arg1, arg2, *, flag=False): pass >>> fun(1,2,3) Traceback (most
recent call last): File “<stdin>”, line 1, in <module> TypeError: fun() takes exactly 2 positional arguments (3 given)
annotations
attaching metadata to arguments
syntax: def func(arg: expression)->returnValue: pass
expressions can be anything
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”: ....
Python ignore annotations
stored in __annotations__
3rd party tools: optimizers, docs, IDEs, typechecking, ...
files
text vs binary text files must have encoding produce unicode
binary files produce bytestrings
new I/O layer raw I/O buffered I/O text I/O
modules
absolute imports
import foo always imports from the top level
import from same package: from . import foo
import from parent package: from .. import foo
stdlib
goals: PEP8 compliance some structure cleaning out cruft
PEP 0008 “Modules should have short, all-lowercase names.”
BaseHTTPServer cPickle cStringIO HTMLParser ....
either renamed, or Flushed.
structure
current structure
duplication and near duplications bsddb gdbm dbm dumbdbm
urllib vs urllib2
new packages: http html xmlrpc json dbm urllib
lots of renames: cStringIO -> io.StringIO SocketServer -> socketserver httplib
-> http.client urllib + urllib2 -> urllib
cruft
lots of purges: old email modules old hash modules old
platform modules
thread gone use threading
UserDict and friends -> subclass from dict
porting approach
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
2.x -> 2.6 -> 3.0
python 2.6 interim release -3 flag: turns on warnings from
__future__ from future_builtins import enables some backports
2to3 ships with 2.6, 3.0 does mechanical rewrites handles a
lot never going to be perfect
unit tests
eventually ... drop 2.x version switch to 3.0 version
things not in py3k
antigravity xkcd#353
case insensitivity death to lambda implicit self macros
not a complete rewrite
what happens to python 2.x?
None
2.x is not going away will continue to be supported
please try 3.0 betas
(and report bugs and send fixes)
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/
exit()
More Decks by Abhinav Sarkar
See All by Abhinav Sarkar
A Tiny Bytecode VM for Arithmetics in Haskell
abhin4v
0
250
Many Ways to Concur
abhin4v
0
1.1k
Moving People with Clojure
abhin4v
1
490
Introduction to Concurrency in Haskell
abhin4v
0
1.5k
A Taste of Clojure
abhin4v
0
110
Other Decks in Programming
See All in Programming
Lean で手続き的プログラム の証明が書けるようになった
inoueasei
11
5.6k
AIエージェントの「テストは通るのに本番で壊れる」を減らす
shibuiwilliam
3
130
joyful-journey-spring-boot.pdf
hollycummins
0
130
手の動かし方がわからなかった私が個人開発者になるまで
usagi0920_pochi
0
350
あなたはMCPの機能を、まだ知らない。
4geru
1
750
Distance field of Julia set
kinakomoti321
0
330
眠らない開発チームの作り方
biwakonbu
0
170
テストだけで見つかる? Rubyでつくるメソッド発掘器
izawa
0
170
なぜ私たちは「現時点では」KMPの導入を見送ったのか
chigichan24
1
650
Stage 3でも仕様は変わる。アプリケーション開発者のためのTC39プロセスの読み方
bicstone
1
1k
vue-tsc x TypeScript7
koukimiura
0
110
作れるエンジニアから、価値を届けるエンジニアへ
ryunakayama
1
1.3k
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
2k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
73
43k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
1
310
Facilitating Awesome Meetings
lara
57
7.2k
Web Performance Is a Journey [PerfNow 2026]
tammyeverts
3
180
Designing for Timeless Needs
cassininazir
1
570
Leo the Paperboy
mayatellez
11
2.5k
Navigating Weather and Climate Data
rabernat
0
610
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.8k
GitHub's CSS Performance
jonrohan
1033
470k
Statistics for Hackers
jakevdp
799
230k
The Spectacular Lies of Maps
axbom
PRO
1
1.2k