1. Why Python 3!
Finally Possible
Wednesday, October 10, 12
2. Why We Didn’t Switch
Python 3 is incompatible with Python 2
Performance was worse
Memory usage was higher
Most libraries and frameworks were not
ported
Wednesday, October 10, 12
3. Performance
Python 3.0 had a whole new IO library
Much cleaner, but...
Entirely written in Python
Fixed in Python 3.1
Wednesday, October 10, 12
4. Memory Usage
Mainly caused by all-Unicode all-the-time
Every string took 2 or 4 bytes per character
Often memory requirements for an app
doubled or worse
Fixed in Python 3.3
Wednesday, October 10, 12
5. Libraries and
Frameworks
NumPy 1.5+ and SciPy 0.9+
Django 1.5+
Pyramid 1.3+
Six library used as a compatibility layer by
many libraries now
Wednesday, October 10, 12
6. OK, But Why Bother?
Cleaned up syntax, better library
organization, blah, blah, blah. But it does
make things better going forward.
Simpler string handling
Lot’s of cool new features
Wednesday, October 10, 12
7. Cleaned Up Syntax
except Exception,e -> except Exception as e
True, False and None are reserved words
now. Whew, that could have been ugly!
All classes are “new” classes
super() instead super(parent,self)
Annotations are cool!
Wednesday, October 10, 12
8. Simpler String Handling
All strings are Unicode
No more worrying about codecs
Flexible String Representation in Python 3.3
solves memory issues
Wednesday, October 10, 12
9. More Comprehensions
There are now dict and set comprehensions
{k: v for k, v in stuff if v} (Python 2.7+)
{k for k in list_of_stuff if k>0}
Wednesday, October 10, 12
10. New Syntax Tricks
Sets: {1,2,3}
Keyword only arguments:
def awesome(name, *, pumpItUp=False)
Extended unpacking:
a, b, *rest = range(5)
nonlocal is like global but for the enclosure
Wednesday, October 10, 12
11. Chained Exceptions
try:
a = divide(1,0)
except Exception as e:
raise ThatsNotGood() from e
Traceback shows both exceptions
Both exceptions also shown if you have an
exception in your exception handler
Wednesday, October 10, 12
12. Ordered Dictionaries
Normal dictionaries are random order
Can be a real problem for reading and
writing JSON since keys get scrambled
Ordered Dictionaries preserve insertion
order
Used by the JSON library
Wednesday, October 10, 12
13. Formatting Numbers
Easy to get commas now:
format(1234567, ‘,d’) -> ‘1,234,567’
format(1234567.89, ‘,.2f’) -> ‘1,234,567.89’
Wednesday, October 10, 12
14. Context Manager
Back-ported to Python 2.6 so use it now!
Manages lifetime. Use instead of try/finally.
with open(‘mylog.txt’) as infile:
error_lines=[]
for line in infile:
if ‘ERROR’ in line:
error_lines.append(line)
File closed as soon as with block ends
Wednesday, October 10, 12
15. contextlib
from contextlib import contextmanager
@contextmanager
def db_transaction(connection):
cursor = connection.cursor()
try:
yield cursor
except:
connection.rollback()
raise
else:
connection.commit()
db = DatabaseConnection()
with db_transaction(db) as cursor:
...
Wednesday, October 10, 12
16. argparse
Much better library for handling command-
line arguments
Automatically generates help
Available for Python 2.6 so use it now
Really, check it out!
Wednesday, October 10, 12
17. concurrent.futures
Simplifies multithreading:
import concurrent.futures, shutil
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as e:
e.submit(shutil.copy, 'src1.txt', 'dest1.txt')
e.submit(shutil.copy, 'src2.txt', 'dest2.txt')
e.submit(shutil.copy, 'src3.txt', 'dest3.txt')
e.submit(shutil.copy, 'src4.txt', 'dest4.txt')
Wednesday, October 10, 12
18. New GIL
GIL is the great Achilles Heal of Python
Old GIL had pretty bad performance on
multicore boxes
New GIL in Python 3.2 is generally much
better. Performance was enough better for
me to abandon gevent!
Can still be ugly if you mix CPU-bound and
IO-bound code
Wednesday, October 10, 12
19. Annotations
Let you add some purely documentary tags
onto parameters and functions:
def digest(stuff: “iterable of numbers”,
non_zero:bool=False) -> hash:
Wednesday, October 10, 12
20. New in Python 3.3
venv - virtualenv is baked in now
Fancy new I/O exceptions. Instead of
OSError, now we get things like:
PermissionError
FileNotFoundError
More compact attribute dictionaries
Wednesday, October 10, 12
21. Biggest Gotcha Award
‘print’ is a function now
dict.keys(), dict.values() and dict.items() return
generators instead of lists
Wednesday, October 10, 12
22. Questions?
Want to see an argparse example?
Email me: scott@codecobblers.com
Wednesday, October 10, 12