Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Python 3000


Published on

TimChen (from KaLUG) on the near-to-release Python 3000

Published in: Technology
  • Be the first to comment

Python 3000

  1. Python 3.0 (Py3K) Introduction <ul><ul><li>Authors: ( 使徒提姆 ) </li></ul></ul><ul><ul><li>Location: Taiwan COSCUP 2008 </li></ul></ul><ul><ul><li>Date: 2008/08/24 </li></ul></ul><ul><ul><li>Blog: </li></ul></ul><ul><ul><li>This document has licensed as CC-SA 2.0 </li></ul></ul>
  2. Topic <ul><li>Introduction </li></ul><ul><li>New syntax </li></ul><ul><li>New Feature </li></ul><ul><li>Migratation </li></ul>
  3. Introduction <ul><li>Python is a dynamic object-oriented programming language that can be used for many kinds of software development. </li></ul><ul><li>Last Stable Release: Python 2.5.2 (February 22, 2008) </li></ul><ul><li>Last Beta Release: Python 2.6beta2 and 3.0beta2 (July 17, 2008) </li></ul><ul><li>Python 3000, Python 3.0 and Py3K are all names for the same thing. </li></ul>
  4. Introduction (cont.) <ul><li>The actual Python release will be referred as Python 3.0. </li></ul><ul><li>Sep 03 2008: Python 2.6 and 3.0 final released. (planned) </li></ul><ul><li>Python 2.6: a transitional release to help prepare code for Python 3.0. </li></ul>
  5. Python 3.0 IS... <ul><li>backwards-incompatible. </li></ul><ul><li>remove some previous design mistakes. </li></ul><ul><li>reviewing and improving standard library modules. </li></ul><ul><li>django and some python modules may have tools to generate python 2.x/3.0 code from single code base. </li></ul>
  6. What is NOT changed <ul><li>Guido said in PEP 3099. (PEP: Python Enhancement Proposals) </li></ul><ul><li>Python 3000 will NOT be a rewrite from scratch. </li></ul><ul><li>Python 3000 will NOT be case-insensitive. </li></ul><ul><li>self will NOT become implicit. </li></ul>
  7. What is NOT changed (cont.) <ul><li>lambda will NOT be renamed. </li></ul><ul><li>NO braces. (from __future__ import braces) </li></ul><ul><li>The global statement WILL stay. </li></ul>
  8. print() <ul><li>from statement to function </li></ul><ul><li>2.x you can: print(&quot;Hello&quot;) or print &quot;Hello&quot; </li></ul><ul><li>3.0 you have to add () </li></ul><ul><li>raw_input(now input) was a function </li></ul><ul><li>print >> open(&quot;test.txt&quot;,&quot;w&quot;), &quot;1&quot;,&quot;2&quot;,&quot;3&quot; -> (X) </li></ul><ul><li>print(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, file=open(&quot;test.txt&quot;,&quot;w&quot;)) -> (O) </li></ul>
  9. format() <ul><li>in python 2.x:: </li></ul><ul><ul><li>t=[1,2,3];&quot;abc%s&quot; % t -> 'abc[1, 2, 3]' </li></ul></ul><ul><ul><li>t=(1,2,3);&quot;abc%s&quot; % t -> TypeError </li></ul></ul><ul><li>format() #PEP 3101 </li></ul><ul><li>can define __format__() special method </li></ul><ul><li>&quot;%14.12s&quot; % &quot;hello world! hello!&quot; </li></ul><ul><li>==> </li></ul><ul><li>format(&quot;Hello world! Hello World!&quot;,&quot;>14.12&quot;) </li></ul>
  10. Python 3.0 Hello World 101 <ul><li>print(&quot;hello world!&quot;) </li></ul><ul><li>(lambda:print(&quot;hello world!&quot;))() </li></ul><ul><li>print(&quot;hello {what}&quot;.format(what='world!')) </li></ul><ul><li>print(&quot;{0} {1}&quot;.format('hello','world!')) </li></ul><ul><li>print(&quot;hello {}&quot;.format(open('world!', 'w'))) </li></ul><ul><li>print(&quot;hello {0[what]}&quot;.format(dict(what='world!'))) </li></ul><ul><li>print(&quot;hello {0[what]}&quot;.format({'what':'world!'})) </li></ul>
  11. unicode <ul><li>unicode(charset) is not utf-8 (bytestring). </li></ul><ul><li>unicode == str now. (no unicode type in 3.0) </li></ul><ul><li>add a bytes type, bytes('test','utf-8') </li></ul><ul><li>'開源'.encode('utf-8')==> b'xe9x96x8bxe6xbax90' </li></ul><ul><li>'開源'.encode('cp950')==> b'xb6}xb7xbd' </li></ul><ul><li>Unicode(2.x) -> str(3.x) </li></ul><ul><li>str(2.x) -> bytes(3.x) </li></ul>
  12. abc,collections <ul><li>PEP 3119 </li></ul><ul><li>Abstract Base Class </li></ul><ul><li>module collections </li></ul><ul><li>OOP term 'Invocation' : object.method() ==> implies polymorphis </li></ul><ul><li>polymorphism: 'type-of object'.method() : may run different code when types different. </li></ul><ul><li>duck typing polymorphism: assume object is a duck if it can quacks. -- inspection style, check from external. (did not use object's method to check) </li></ul>
  13. a situation without using abc <ul><li>Situation: you want to accept iterables from one of following datatypes: dict or tuples. </li></ul><ul><li>you use hasattr(my_iterables,'items') </li></ul><ul><li>may lead to unexpected behavior. </li></ul><ul><li>does it support dict-like objects? isn't that main advantages to use duck-typing ? </li></ul><ul><li>what is a dict? </li></ul>
  14. abc,collections (cont.) <ul><li>there maybe a dict without __contains__ and have has_key() function. </li></ul><ul><li>here comes abc ==> Abstract base classes </li></ul><ul><li>what to inspect? there are too many stuffs in a python object to check. </li></ul><ul><li>abc and collection modules intends to answer these questions: </li></ul><ul><ul><li>what is a 'MutableMapping' ? </li></ul></ul><ul><ul><li>what is a 'Iterable' ? </li></ul></ul><ul><ul><li>and what is a 'MyAbstractType' ? </li></ul></ul>
  15. abc,collections (cont.) <ul><li>isinstance([], collections.MutableSequence) </li></ul><ul><li>@abstractmethod, @abstractproperty </li></ul>
  16. abc example <ul><li> </li></ul><ul><li>Python2 </li></ul><ul><ul><li>try: </li></ul></ul><ul><ul><li>iter(obj) </li></ul></ul><ul><ul><li>except TypeError: </li></ul></ul><ul><ul><li>do_something_with_not_iterable_object(obj) </li></ul></ul><ul><ul><li>else: </li></ul></ul><ul><ul><li>do_something_with_iterable_object(obj) </li></ul></ul>
  17. abc example (cont.) <ul><li>Python3 </li></ul><ul><ul><li>from collections import Iterable </li></ul></ul><ul><ul><li>if isinstance(obj, Iterable): </li></ul></ul><ul><ul><li>the_object_is_iterable() </li></ul></ul><ul><ul><li>else: </li></ul></ul><ul><ul><li>the_object_is_not_iterable() </li></ul></ul>
  18. abc (cont.) <ul><li>your custom class doesn't need to inherit from the base class. </li></ul><ul><li>how to do it? </li></ul><ul><li>collections.Iterable.register(myclass) </li></ul><ul><li>how to register your module to abc: </li></ul><ul><li>from _yourlinkedlist import YourLinkedList </li></ul><ul><li>from collections import Sequence </li></ul><ul><li>Sequence.register(YourLinkedList) </li></ul>
  19. abc (cont.) <ul><li>your C extension module works great. </li></ul><ul><li>io.IOBase, io.BufferedIOBase </li></ul><ul><li>StringIO,cStringIO --> io </li></ul><ul><li>abc --> the pythonic way to do both traditional OOP and duck-typing polymorph in the same time. </li></ul><ul><li>&quot;There should be one and preferably only one obvious way to do it&quot;. </li></ul>
  20. nonlocal <ul><li>PEP 3104 </li></ul><ul><li>python 2.1+ name resolution: LEGB (local => enclosed => global => builtins) </li></ul><ul><li>nested scoping was added when python 2.1 introduced. </li></ul><ul><li>local was always first checked, so if you assigned a local varible in a scope it assume that 'name' is a local, it's too bad for a closure. </li></ul><ul><li>there was some working arounds, however python 3.0 add a keyword: nonlocal. </li></ul>
  21. closure (it's read-only) <ul><li>python 2.x </li></ul><ul><li>def get_counter_py2(): </li></ul><ul><li>x = 0 </li></ul><ul><li>def f(): </li></ul><ul><li>x += 1 </li></ul><ul><li>return x </li></ul><ul><li>return f </li></ul><ul><li>add1 = get_counter_py2() </li></ul><ul><li>print add1();print add1();print add1() </li></ul>
  22. closure (it's read-only) <ul><li>python 2.x error:: </li></ul><ul><li>Traceback (most recent call last): </li></ul><ul><li>File &quot;;, line 10, in <module> </li></ul><ul><li>print next() </li></ul><ul><li>File &quot;;, line 4, in f </li></ul><ul><li>x += 1 </li></ul><ul><li>UnboundLocalError: </li></ul><ul><li>local variable 'x' referenced before assignment </li></ul>
  23. closure(cont.) <ul><li>closure working around in 2.x (1):: </li></ul><ul><li>def get_counter_traditional(): </li></ul><ul><li>x = [0] </li></ul><ul><li>def f(): </li></ul><ul><li>x[0] += 1 </li></ul><ul><li>return x[0] </li></ul><ul><li>return f </li></ul>
  24. closure(cont.) <ul><li>closure working around in 2.x (2):: </li></ul><ul><li>def get_counter_neat(): </li></ul><ul><li>def f(): </li></ul><ul><li>f.x += 1 </li></ul><ul><li>return f.x </li></ul><ul><li>f.x = 0 </li></ul><ul><li>return f </li></ul><ul><li>&quot;To describe something as clever is NOT considered a compliment in the </li></ul><ul><li>Python culture.&quot; -- Alex Martelli (Python Cookbook 2nd.) </li></ul>
  25. closure with nonlocal(cont.) <ul><li>py3k used nonlocal:: </li></ul><ul><li>def get_counter_py3k(): </li></ul><ul><li>x=0 </li></ul><ul><li>def f(): </li></ul><ul><li>nonlocal x </li></ul><ul><li>x += 1 </li></ul><ul><li>return x </li></ul><ul><li>return f </li></ul><ul><li>add1 = get_counter_py3k() </li></ul><ul><li>print(add1());print(add1());print(add1()) </li></ul>
  26. closure with nonlocal(cont.) <ul><li>py3k used nonlocal:: </li></ul><ul><li>def get_counter_py3k(x): </li></ul><ul><li>def f(): </li></ul><ul><li>nonlocal x </li></ul><ul><li>x += 1 </li></ul><ul><li>return x </li></ul><ul><li>return f </li></ul><ul><li>next = get_counter_py3k(1) print(add1());print(add1());print(add1()) </li></ul><ul><li>* &quot;There should be one and preferably only one obvious way to do it&quot;. </li></ul>
  27. function annotations <ul><li>PEP 3107 -- completely optional. </li></ul><ul><li>def foo(a: 'x' , b: 5 + 6 , c: list = [5,2,3]) -> max(2, 9): pass </li></ul><ul><li>foo.__annotations__ -> {'a': 'x', 'c': <class 'list'>, 'b': 11, 'return': 9} </li></ul><ul><li>you could think as a compile time comment for function. </li></ul><ul><li>intend to used by third-party libraries. ie: typecheck </li></ul><ul><li>&quot;There should be one and preferably only one obvious way to do it&quot;. </li></ul>
  28. Misc. <ul><li>__package__:: </li></ul><ul><li>PEP 366 -- Main module explicit relative imports </li></ul><ul><li>Extended unpacking:: </li></ul><ul><li>x,*y = range(6) </li></ul><ul><li>a,b,*c = {1:2,3:4,5:6} </li></ul>
  29. Misc. (cont.) <ul><li>map() filter() reduce() and list compresion:: </li></ul><ul><li>map() and filter() becomes iterator and still in builtins </li></ul><ul><li>reduce() placed in functools module </li></ul><ul><li>range():: </li></ul><ul><li>next(range(10)) : TypeError: range object is not an iterator </li></ul>
  30. migrate py3k use 2to3 <ul><li>* /usr/bin/2to3 (/usr/local/bin/2to3) </li></ul><ul><li>2to3 -w </li></ul><ul><li>--- (original) </li></ul><ul><li>+++ (refactored) </li></ul><ul><li>@@ -1,1 +1,1 @@ </li></ul><ul><li>-print 'hi' </li></ul><ul><li>+print('hi') </li></ul><ul><li>RefactoringTool: Files that were modified: </li></ul><ul><li>RefactoringTool: </li></ul>
  31. Conclude <ul><li>a lot of stuff changed. (there're still lots of stuff we don't mention changed!) </li></ul><ul><li>you could still use python 2.x and wait python 3.x matures. </li></ul><ul><li>2.6+ will recieve backports from python 3.0 </li></ul><ul><li>from __future__ import py3k </li></ul>