Why Python 3

22
Why Python 3! Finally Possible Wednesday, October 10, 12

description

Scott Maxwell's presentation "Why Python 3" at the San Diego Python Meetup in September, 2012.

Transcript of Why Python 3

Page 1: Why Python 3

Why Python 3!Finally Possible

Wednesday, October 10, 12

Page 2: Why Python 3

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

Page 3: Why Python 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

Page 4: Why Python 3

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

Page 5: Why Python 3

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

Page 6: Why Python 3

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

Page 7: Why Python 3

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

Page 8: Why Python 3

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

Page 9: Why Python 3

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

Page 10: Why Python 3

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

Page 11: Why Python 3

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

Page 12: Why Python 3

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

Page 13: Why Python 3

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

Page 14: Why Python 3

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

Page 15: Why Python 3

contextlibfrom contextlib import contextmanager

@contextmanagerdef 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

Page 16: Why Python 3

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

Page 17: Why Python 3

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

Page 18: Why Python 3

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

Page 19: Why Python 3

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

Page 20: Why Python 3

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

Page 21: Why Python 3

Biggest Gotcha Award

‘print’ is a function now

dict.keys(), dict.values() and dict.items() return generators instead of lists

Wednesday, October 10, 12

Page 22: Why Python 3

Questions?

Want to see an argparse example?

Email me: [email protected]

Wednesday, October 10, 12