Moving To Python 3 Why? When? How? Nick Efford (http://about.me/nickefford)
Backwards Compatibility An ‘unwritten law’ of software... ...and yet Python 3 breaks it – so badly that “Hello World!” no ...
Compatibility Isn’t Free... Java is bloated because obsolete features are never removed
Python’s Brave Decision "The language has two choices: either continue to bear the burden of what are now considered ...
Desirable Language Attributes <ul><li>Minimal redundancy
Maximal separation of concerns
Minimal surprise
Conceptual consistency
Appropriate complexity </li></ul>
Redundancy: Iteration in Java for (int i = 0; i < message.length(); ++i) { System.out.println(message.charAt(i)); } for (i...
Redundancy: Iteration in Java for (int i = 0; i < vec.size(); ++i) { System.out.println(vec.elementAt(i)); } for (int i = ...
Redundancy: Iteration in Java for (int number : vec) { System.out.println(number); } for (char character : messageChars) {...
Redundancy: Iteration in Java // Before JDK 1.5 BufferedReader inputFile = new BufferedReader( new FileReader(&quot;foo.tx...
Redundancy: Iteration in Python for character in string: print character for number in numbers: print number input_file = ...
Redundancy: Integer Representation Java: four primitive integer types, four corresponding wrapper classes,  BigInteger  cl...
Redundancy: Object Model Two types of class in Python 2: class Foo: ... class Bar(object): ... Only ‘new-style’ classes ex...
Redundancy: Console Input Python 2 has  two  functions providing console input: raw_input , yielding input as a string inp...
Conceptual Consistency: I/O In Python 2,  print  is a statement In Python 3, it is a  function Benefits: <ul><ul><li>Consi...
Greater flexibility provided by keyword arguments </li></ul></ul>print(text, file=output_file)   print(x, y, z, sep=':')
Minimal Surprise: Integer Division >>> 5 / 3 1.6666666666666667 >>> 5 // 3 1 Python 3 >>> 5 / 3 1 Python 2 (like C, C++, J...
Upcoming SlideShare
Loading in …5
×

Moving to Python 3

1,807 views
1,665 views

Published on

A presentation to the West Yorkshire Python user group on moving from Python 2 to Python 3.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,807
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
17
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Moving to Python 3

  1. 1. Moving To Python 3 Why? When? How? Nick Efford (http://about.me/nickefford)
  2. 2. Backwards Compatibility An ‘unwritten law’ of software... ...and yet Python 3 breaks it – so badly that “Hello World!” no longer works! $ python3 hello.py File &quot;hello.py&quot;, line 1 print 'Hello World!' ^ SyntaxError: invalid syntax Why?
  3. 3. Compatibility Isn’t Free... Java is bloated because obsolete features are never removed
  4. 4. Python’s Brave Decision &quot;The language has two choices: either continue to bear the burden of what are now considered poor design decisions... or suck it up and let us try and fix some of these problems. It's like going to the dentist; it may hurt, but if that minor toothache goes untreated and develops into an abscess, you will wish you were dead.&quot; – blog entry by Collin Winter
  5. 5. Desirable Language Attributes <ul><li>Minimal redundancy
  6. 6. Maximal separation of concerns
  7. 7. Minimal surprise
  8. 8. Conceptual consistency
  9. 9. Appropriate complexity </li></ul>
  10. 10. Redundancy: Iteration in Java for (int i = 0; i < message.length(); ++i) { System.out.println(message.charAt(i)); } for (int i = 0; i < messageChars.length; ++i) { System.out.println(messageChars[i]); } 2 different ‘classic’ syntaxes for iterating over strings & arrays...
  11. 11. Redundancy: Iteration in Java for (int i = 0; i < vec.size(); ++i) { System.out.println(vec.elementAt(i)); } for (int i = 0; i < vec.size(); ++i) { System.out.println(vec.get(i)); } Enumeration<Integer> enumerator = vec.elements(); while (enumerator.hasMoreElements()) { System.out.println(enumerator.nextElement()); } Iterator<Integer> iterator = vec.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } ...+ 4 for vectors...
  12. 12. Redundancy: Iteration in Java for (int number : vec) { System.out.println(number); } for (char character : messageChars) { System.out.println(character); } ...+ 2 newer approaches for vectors & arrays...
  13. 13. Redundancy: Iteration in Java // Before JDK 1.5 BufferedReader inputFile = new BufferedReader( new FileReader(&quot;foo.txt&quot;)); String line = inputFile.readLine(); while (line != null) { System.out.println(line); line = inputFile.readLine(); } // Since JDK 1.5 Scanner inputFile = new Scanner(new File(&quot;foo.txt&quot;)); while (inputFile.hasNextLine()) { System.out.println(inputFile.nextLine()); } ...+ 2 for text files = 10 different iteration syntaxes!
  14. 14. Redundancy: Iteration in Python for character in string: print character for number in numbers: print number input_file = open('foo.txt') for line in input_file: print line, One syntax works for strings, lists and text files!
  15. 15. Redundancy: Integer Representation Java: four primitive integer types, four corresponding wrapper classes, BigInteger class Python 2: two integer types – int & long Python 3: one integer type – int
  16. 16. Redundancy: Object Model Two types of class in Python 2: class Foo: ... class Bar(object): ... Only ‘new-style’ classes exist in Python 3 (either syntax can be used) ‘old-style’ class ‘new-style’ class
  17. 17. Redundancy: Console Input Python 2 has two functions providing console input: raw_input , yielding input as a string input , yielding the result of calling eval on input Python 3 has one function, input , with the same behaviour as Python 2’s raw_input (New programmers also find this less surprising...)
  18. 18. Conceptual Consistency: I/O In Python 2, print is a statement In Python 3, it is a function Benefits: <ul><ul><li>Consistency with console input (also a function)
  19. 19. Greater flexibility provided by keyword arguments </li></ul></ul>print(text, file=output_file) print(x, y, z, sep=':')
  20. 20. Minimal Surprise: Integer Division >>> 5 / 3 1.6666666666666667 >>> 5 // 3 1 Python 3 >>> 5 / 3 1 Python 2 (like C, C++, Java – but unlike proper arithmetic)
  21. 21. Separation of Concerns: Text vs Binary Data Python 2: str for ASCII text strings & byte strings unicode for Unicode strings Two representations of text, overlapping with one for binary data! Python 3: str for Unicode text strings bytes for strings of bytes Clean separation of text and binary representations, with encode & decode methods for conversion
  22. 22. So Python 3... <ul><li>Is less redundant
  23. 23. Is less surprising
  24. 24. Is more consistent
  25. 25. Separates concerns more cleanly </li></ul>
  26. 26. Time To Change? <ul><li>Python 3 has matured to the point where it is now ‘production-ready’
  27. 27. Development of the Python 2 line has come to an end with Python 2.7 (apart from bug fixes)
  28. 28. Focus for future innovation will be Python 3 (notwithstanding Jython, IronPython, PyPy, etc) </li></ul>
  29. 29. A Helpful Tool: 2to3 <ul><li>Distributed with Python 3
  30. 30. Converts Python 2 code to Python 3
  31. 31. Integrates with D istutils and can run during installation, allowing you to support 2 & 3 from one codebase
  32. 32. Cannot do a perfect job; you may need more unit tests and may have to refactor your code a bit
  33. 33. Only handles Python code; C extensions need to be converted to Python 3 API by hand </li></ul>
  34. 34. Obstacles <ul><li>Support from third-party libraries & frameworks – ‘chicken & egg’ situation
  35. 35. Availability of books, online tutorials, etc
  36. 36. Running multiple Python versions alongside each other is a pain ( virtualenv helps a lot) </li></ul>

×