Your SlideShare is downloading. ×

Why Python 3

404

Published on

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

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

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
404
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Why Python 3! Finally PossibleWednesday, 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 portedWednesday, 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.1Wednesday, 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.3Wednesday, 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 nowWednesday, 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 featuresWednesday, 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 issuesWednesday, 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 enclosureWednesday, 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 handlerWednesday, 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 libraryWednesday, 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 endsWednesday, 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 codeWednesday, 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 dictionariesWednesday, October 10, 12
  • 21. Biggest Gotcha Award ‘print’ is a function now dict.keys(), dict.values() and dict.items() return generators instead of listsWednesday, October 10, 12
  • 22. Questions? Want to see an argparse example? Email me: scott@codecobblers.comWednesday, October 10, 12

×