Python 2 vs. Python 3
Changes, support and porting
Gerald Senarclens de Grancy, gerald@senarclens.eu
Linuxtage, April 2013, Graz
Short version:
Python 2.x is the status quo.
Python 3.x is the present and future of the language.
Current Linux distributions offer many Python 3 libraries
600+ libraries/modules already ported to Python 3 on PyPI
sudo pip-${PYTHON-VERSION} install ${PACKAGE}
Use a good reference! (available as Android app ;)
u'...'
is obsoleteprint
is now a function
print >>sys.stderr, 1, 2, 3
⇒
print(1, 2, 3, file=sys.stderr)
sys.stderr.write(.)
xrange
⇒ range
list()
if required (e.g. in interactive sessions)except (RuntimeError, ImportError), e
except (RuntimeError, ImportError) as e
# Python 3 differences a_list = range(0, 10, 2) squares = [x ** 2 for x in range(10)] try: 1 / 0 except (ZeroDivisionError, AttributeError), e: pass a_string = u"text"
# Python 3 differences a_list = range(0, 10, 2) a_list = list(range(0, 10, 2)) squares = [x ** 2 for x in range(10)] try: 1 / 0 except (ZeroDivisionError, AttributeError) as e: pass a_string = "text"
long
data type removed
sys.maxint
⇒ sys.maxsize
from . import submodule
__nonzero__(self)
⇒ __bool__(self)
class Stack(object): """ Stump of a single stack in a pre-marshalling problem. A stack can only be filled or emptied from top. Each stack has a given maximum high. The highest priority of any container on the stack is 1. Increasing numbers mean decreasing priority. """ def __init__(self, max_size): """ Initialize a new empty stack. """ self._containers = [] self._max_size = max_size self._steps = 0 def __len__(self): """ Use the container list's len. """ return len(self._containers) def __bool__(self): """ All existing stacks are True. This is important to be able to test for existance. Otherwise empty stacks would evaluate to False (as __len__ is the backup for evaluating truth values and empty stacks would be False. """ return True def search_final_stack(container): """ Return stack if found, otherwise None. Dummy implementation. """ return stack stack = Stack(5) for _i in range(5): final_stack = search_final_stack(3) if final_stack: print("found a stack") else: print("no final stack available")
reduce(.)
functools.reduce(.)
iterator.next()
⇒ next(iterator)