26
10 Reasons to Adopt Python 3

10 reasons to adopt Python 3

  • Upload
    bleemeo

  • View
    65

  • Download
    1

Embed Size (px)

Citation preview

Page 1: 10 reasons to adopt Python 3

10 Reasons to Adopt Python 3

Page 2: 10 reasons to adopt Python 3

About Me

Pierre Fersing – [email protected]

CTO & co-founder @BleemeoPython & Django dev for 10 years

Page 3: 10 reasons to adopt Python 3

Monitoring as a Service solution

API in Django + User Interface in ReactJS

Page 4: 10 reasons to adopt Python 3

① Comparison in Python

>>> ["user1", 20] < ["user2", 10]True>>> ["user1", 20] < ["user1", 30]True

Page 5: 10 reasons to adopt Python 3

① Comparison in Python

Py2 : False !Py3 : TypeError: unorderable types: tuple() < list()

>>> ("user1", 20) < ["user2", 99]

>>> max("one", 2)"one"

Page 6: 10 reasons to adopt Python 3

② 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

Page 7: 10 reasons to adopt Python 3

③ Keywords Only Arguments

>>> def sortwords(... words, reverse, case_sensitive):

>>> sortwords(... ["one", "two", "three"],... reverse=True, case_sensitive=False)

Page 8: 10 reasons to adopt Python 3

③ 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):

Page 9: 10 reasons to adopt Python 3

③ Keyword Arguments

>>> sortwords(... "one", "two", "three",... case_sensitive=False... )

>>> def sortwords(*words, **kwargs):... case_sensitive = \... kwargs.pop("case_sensitive")

Page 10: 10 reasons to adopt Python 3

③ Keyword Arguments

>>> sortwords(... "one", "two", "three",... case_sensitive=False... )

>>> def sortwords(... *words, case_sensitive=True):

Page 11: 10 reasons to adopt Python 3

④ Improved Except Syntax

>>> try:... 0 / 0... except OSError, ZeroDivisionError:... print("Error")

Page 12: 10 reasons to adopt Python 3

④ Improved Except Syntax

>>> try:... 0 / 0... except (OSError, ZeroDivisionError) as err:... print("Error")

Page 13: 10 reasons to adopt Python 3

⑤ 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

Page 14: 10 reasons to adopt Python 3

⑤ 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

Page 15: 10 reasons to adopt Python 3

⑥ Reworked OS/IO Exception

>>> try:... fd = open("/etc/shadow")... except IOError:... print("Error")

>>> try:... os.stat("/etc/shadow")... except OSError:... print("Error")

Page 16: 10 reasons to adopt Python 3

⑥ Reworked OS/IO Exception

>>> try:... fd = open("/etc/shadow")... except IOError as err:... if err.errno in (EACCES, EPERM):... print("Error")... else:... raise

Page 17: 10 reasons to adopt Python 3

⑥ Reworked OS/IO Exception

>>> try:... fd = open("/etc/shadow")... except PermissionError:... print("Error")

Page 18: 10 reasons to adopt Python 3

⑦ 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

Page 19: 10 reasons to adopt Python 3

⑧ Stdlib Additions

>>> etc = pathlib.Path("/etc")... file = etc / "passwd"... file.read_text()

>>> @functools.lru_cache(max_size=32)... def expansive_function(params):

Page 20: 10 reasons to adopt Python 3

⑧ Stdlib Additions

>>> subprocess.run(... ["ls", "-l"], timeout=10)

>>> datetime.now().timestamp()

>>> secrets.token_urlsafe()

Page 21: 10 reasons to adopt Python 3

⑧ Stdlib Additions

Lots more :– lzma

– enum

– ipaddress

– faulthandler

– statistics

– ...

Page 22: 10 reasons to adopt Python 3

⑨ 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

Page 23: 10 reasons to adopt Python 3

⑩ Bonus

>>> 1 / 20.5

>>> for x in iterable:... yield x>>> yield from iterable

Page 24: 10 reasons to adopt Python 3

⑩ Bonus

Tab-completion in interpreter

>>> class Children(Parent):... def method(self):... super().method()

Page 25: 10 reasons to adopt Python 3

⑩ Bonus

>>> round(1.5)2>>> round(2.5)2

Page 26: 10 reasons to adopt Python 3

Question ?