10 Reasons to Adopt Python 3
About Me
Pierre Fersing – pierref@bleemeo.com
CTO & co-founder @Bleemeo
Python & Django dev for 10 years
Monitoring as a Service solution
API in Django + User Interface in ReactJS
① Comparison in Python
>>> ["user1", 20] < ["user2", 10]
True
>>> ["user1", 20] < ["user1", 30]
True
① Comparison in Python
Py2 : False !
Py3 : TypeError: unorderable types: tuple() < list()
>>> ("user1", 20) < ["user2", 99]
>>> max("one", 2)
"one"
② Iterators, Iterators Everywhere
Consume about 1,5 GB of memory with Python
2 !
range vs xrange, keys vs iterkeys, values vs
itervalues, items vs iteritems
zip, map, filter
>>> for _ in range(50000000):
... the_answer = 42
③ Keywords Only Arguments
>>> def sortwords(
... words, reverse, case_sensitive):
>>> sortwords(
... ["one", "two", "three"],
... reverse=True, case_sensitive=False)
③ Keywords Only Arguments
>>> def sortwords(
... words, reverse, case_sensitive):
>>> sortwords(
... ["one", "two", "three"], True, False)
>>> def sortwords(
... words, case_sensitive, reverse):
>>> def sortwords(
... words, *, case_sensitive, reverse):
③ Keyword Arguments
>>> sortwords(
... "one", "two", "three",
... case_sensitive=False
... )
>>> def sortwords(*words, **kwargs):
... case_sensitive = 
... kwargs.pop("case_sensitive")
③ Keyword Arguments
>>> sortwords(
... "one", "two", "three",
... case_sensitive=False
... )
>>> def sortwords(
... *words, case_sensitive=True):
④ Improved Except Syntax
>>> try:
... 0 / 0
... except OSError, ZeroDivisionError:
... print("Error")
④ Improved Except Syntax
>>> try:
... 0 / 0
... except (OSError, ZeroDivisionError) as err:
... print("Error")
⑤ Chained Exceptions
>>> try:
... connection = do_connect()
... # do stuff
... finally:
... connection.close()
Traceback (most recent call last):
File "code.py", line 5, in <module>
connection.close()
NameError: name 'connection' is not defined
⑤ Chained Exceptions
Traceback (most recent call last):
File "plop.py", line 2, in <module>
connection = do_connect()
ConnectionRefusedError: [Errno 111]
Connection refused
During handling of the above exception,
another exception occurred:
Traceback (most recent call last):
File "plop.py", line 5, in <module>
connection.close()
NameError: name 'connection' is not defined
⑥ Reworked OS/IO Exception
>>> try:
... fd = open("/etc/shadow")
... except IOError:
... print("Error")
>>> try:
... os.stat("/etc/shadow")
... except OSError:
... print("Error")
⑥ Reworked OS/IO Exception
>>> try:
... fd = open("/etc/shadow")
... except IOError as err:
... if err.errno in (EACCES, EPERM):
... print("Error")
... else:
... raise
⑥ Reworked OS/IO Exception
>>> try:
... fd = open("/etc/shadow")
... except PermissionError:
... print("Error")
⑦ Reworked Stdlib Names
Which module ? urllib, urllib2, urlparse ?
– Parsing an url : urlparse
– Quoting an URL : urllib
– Do a requests : urllib2
No more cPickle, cProfile, cStringIO
⑧ Stdlib Additions
>>> etc = pathlib.Path("/etc")
... file = etc / "passwd"
... file.read_text()
>>> @functools.lru_cache(max_size=32)
... def expansive_function(params):
⑧ Stdlib Additions
>>> subprocess.run(
... ["ls", "-l"], timeout=10)
>>> datetime.now().timestamp()
>>> secrets.token_urlsafe()
⑧ Stdlib Additions
Lots more :
– lzma
– enum
– ipaddress
– faulthandler
– statistics
– ...
⑨ asyncio and async/await
>>> reader,writer = await asyncio.open_connection(
... "www.python.org", 80)
... writer.write(b'GET / […]')
... async for line in reader:
... # do something with the line
⑩ Bonus
>>> 1 / 2
0.5
>>> for x in iterable:
... yield x
>>> yield from iterable
⑩ Bonus
Tab-completion in interpreter
>>> class Children(Parent):
... def method(self):
... super().method()
⑩ Bonus
>>> round(1.5)
2
>>> round(2.5)
2
Question ?

10 reasons to adopt Python 3

  • 1.
    10 Reasons toAdopt Python 3
  • 2.
    About Me Pierre Fersing– pierref@bleemeo.com CTO & co-founder @Bleemeo Python & Django dev for 10 years
  • 3.
    Monitoring as aService solution API in Django + User Interface in ReactJS
  • 4.
    ① Comparison inPython >>> ["user1", 20] < ["user2", 10] True >>> ["user1", 20] < ["user1", 30] True
  • 5.
    ① Comparison inPython Py2 : False ! Py3 : TypeError: unorderable types: tuple() < list() >>> ("user1", 20) < ["user2", 99] >>> max("one", 2) "one"
  • 6.
    ② Iterators, IteratorsEverywhere Consume about 1,5 GB of memory with Python 2 ! range vs xrange, keys vs iterkeys, values vs itervalues, items vs iteritems zip, map, filter >>> for _ in range(50000000): ... the_answer = 42
  • 7.
    ③ Keywords OnlyArguments >>> def sortwords( ... words, reverse, case_sensitive): >>> sortwords( ... ["one", "two", "three"], ... reverse=True, case_sensitive=False)
  • 8.
    ③ Keywords OnlyArguments >>> def sortwords( ... words, reverse, case_sensitive): >>> sortwords( ... ["one", "two", "three"], True, False) >>> def sortwords( ... words, case_sensitive, reverse): >>> def sortwords( ... words, *, case_sensitive, reverse):
  • 9.
    ③ Keyword Arguments >>>sortwords( ... "one", "two", "three", ... case_sensitive=False ... ) >>> def sortwords(*words, **kwargs): ... case_sensitive = ... kwargs.pop("case_sensitive")
  • 10.
    ③ Keyword Arguments >>>sortwords( ... "one", "two", "three", ... case_sensitive=False ... ) >>> def sortwords( ... *words, case_sensitive=True):
  • 11.
    ④ Improved ExceptSyntax >>> try: ... 0 / 0 ... except OSError, ZeroDivisionError: ... print("Error")
  • 12.
    ④ Improved ExceptSyntax >>> try: ... 0 / 0 ... except (OSError, ZeroDivisionError) as err: ... print("Error")
  • 13.
    ⑤ Chained Exceptions >>>try: ... connection = do_connect() ... # do stuff ... finally: ... connection.close() Traceback (most recent call last): File "code.py", line 5, in <module> connection.close() NameError: name 'connection' is not defined
  • 14.
    ⑤ Chained Exceptions Traceback(most recent call last): File "plop.py", line 2, in <module> connection = do_connect() ConnectionRefusedError: [Errno 111] Connection refused During handling of the above exception, another exception occurred: Traceback (most recent call last): File "plop.py", line 5, in <module> connection.close() NameError: name 'connection' is not defined
  • 15.
    ⑥ Reworked OS/IOException >>> try: ... fd = open("/etc/shadow") ... except IOError: ... print("Error") >>> try: ... os.stat("/etc/shadow") ... except OSError: ... print("Error")
  • 16.
    ⑥ Reworked OS/IOException >>> try: ... fd = open("/etc/shadow") ... except IOError as err: ... if err.errno in (EACCES, EPERM): ... print("Error") ... else: ... raise
  • 17.
    ⑥ Reworked OS/IOException >>> try: ... fd = open("/etc/shadow") ... except PermissionError: ... print("Error")
  • 18.
    ⑦ Reworked StdlibNames Which module ? urllib, urllib2, urlparse ? – Parsing an url : urlparse – Quoting an URL : urllib – Do a requests : urllib2 No more cPickle, cProfile, cStringIO
  • 19.
    ⑧ Stdlib Additions >>>etc = pathlib.Path("/etc") ... file = etc / "passwd" ... file.read_text() >>> @functools.lru_cache(max_size=32) ... def expansive_function(params):
  • 20.
    ⑧ Stdlib Additions >>>subprocess.run( ... ["ls", "-l"], timeout=10) >>> datetime.now().timestamp() >>> secrets.token_urlsafe()
  • 21.
    ⑧ Stdlib Additions Lotsmore : – lzma – enum – ipaddress – faulthandler – statistics – ...
  • 22.
    ⑨ asyncio andasync/await >>> reader,writer = await asyncio.open_connection( ... "www.python.org", 80) ... writer.write(b'GET / […]') ... async for line in reader: ... # do something with the line
  • 23.
    ⑩ Bonus >>> 1/ 2 0.5 >>> for x in iterable: ... yield x >>> yield from iterable
  • 24.
    ⑩ Bonus Tab-completion ininterpreter >>> class Children(Parent): ... def method(self): ... super().method()
  • 25.
  • 26.