Dr. Brett Cannon
Microsoft (Azure Data Science Tools)
PyCon US 2017
https://docs.python.org/3.6/whatsnew/3.6.html
What's New in Python 3.6
1
Slide 2
Slide 2 text
https://www.python.org/dev/peps
Thanks to the 2016 Python core sprint for work on 12 of the PEPs.
2
Slide 3
Slide 3 text
PEP 468: Preserving keyword argument order
def pep468(**kwargs):
print(list(kwargs.keys()))
>>> pep468(a=1, b=2, c=3)
['a', 'b', 'c']
3
Slide 4
Slide 4 text
PEP 487: Simpler customization of class creation
class PEP487:
def __init_subclass__(cls, whom, **kwargs):
super().__init_subclass__(**kwargs)
cls.hello = lambda: print(f"Hello, {whom}")
>>> class HelloWorld(PEP487, whom="World"):
... pass
>>> HelloWorld.hello()
Hello, World
4
Slide 5
Slide 5 text
PEP 495: Local time disambiguation
dt = datetime.datetime(2016, 11, 6, 1, 30)
pdt = dt.astimezone()
pst = dt.replace(fold=1).astimezone()
>>> pdt.strftime('%Y-%m-%d %T %Z%z')
'2016-11-06 01:30:00 Pacific Summer Time-0700'
>>> pst.strftime('%Y-%m-%d %T %Z%z')
'2016-11-06 01:30:00 Pacific Standard Time-0800'
5
Slide 6
Slide 6 text
PEP 498: Formatted string literals
>>> whom = "PyCon CA"
>>> where = "Toronto", "ON", "Canada"
>>> f"Hello, {whom}! Welcome to {where!r}."
"Hello, PyCon CA! Welcome to ('Toronto', 'ON', 'Canada')."
6
Slide 7
Slide 7 text
• PEP 506
▫ Use the secrets module for anything security- or crypto-related
▫ Use random for modeling and simulation
• PEP 524
▫ In Python 3.5.0, os.urandom() will block until there's enough entropy
▫ In Python 3.5.2, os.urandom() will fall back to /dev/urandom if the
call would block (this is bad if you were expecting crypto-quality bits)
▫ In Python 3.6, os.urandom() goes back to 3.5.0 semantics
▫ The new os.getrandom() raises an exception if it would block
PEP 506/524: Adding a secrets module
& os.urandom() blocks
7
Slide 8
Slide 8 text
• 99.9% of you will not (directly) care about this
▫ Seriously, I can count the people who care about this on one hand
• Dictionaries now have a version ID that’s only accessible from C code
▫ Meant as a monitor to detect when namespaces have mutated
▫ Helpful for caching, e.g. global and built-in namespace lookups
PEP 509: Add a private version to dict
8
Slide 9
Slide 9 text
PEP 515: Underscores in numeric literals
one_billion = 1_000_000_000
black = 0x_FF_FF_FF_FF
9
Slide 10
Slide 10 text
PEP 519: Adding a file system path protocol
os.path.exists(pathlib.Path("What's New in Python 3.6.pptx'"))
class Pathy(os.PathLike):
def __fspath__(self) -> Union[str, bytes]:
return path_repr
10
Slide 11
Slide 11 text
PEP 520: Preserving class attribute definition order
>>> list(OrderPreserved.__dict__.keys())
['__module__', 'a', 'b', 'meth', '__dict__', '__weakref__',
'__doc__']
class OrderPreserved:
a = 1
b = 2
def meth(self):
pass
11
Slide 12
Slide 12 text
• 99% of you won’t care about this either
▫ Only if you are writing a JIT, debugger, or profiler will you care
• There is now a C API that allows for specifying an alternate frame
evaluation function
▫ I.e. PyEval_EvalFrameEx() is essentially pluggable
PEP 523: Adding a frame evaluation API to CPython
12
Slide 13
Slide 13 text
PEP 525/530: Asynchronous generators &
comprehensions
# For use in `async for`.
async def ticker(delay, to):
for i in range(to):
yield i
await asyncio.sleep(delay)
async def async_comprehensions():
comp_with_async = [i async for i in aiter() if i % 2]
comp_with_await = [await fun() for fun in funcs]
13
Slide 14
Slide 14 text
PEP 526: Syntax for variable annotations
module_level: str = "see __annotations__"
class Annotated:
ins: int # Does not exist yet.
ins_with_default: str = "see Annotated.__annotations__"
cls: ClassVar[float] = 3.14
def meth(self):
local: int = 42
does_not_exist_yet: str
14
Slide 15
Slide 15 text
• REPL now uses UTF-8!
• Bytes-based paths are now accepted on Windows
▫ sys.getfilesystemencoding() returns UTF-8
PEP 528/529: Windows default encoding
15
Slide 16
Slide 16 text
• PYTHONMALLOC
▫ You can turn on/off memory debugging hooks
▫ You can force the use of malloc() for all memory allocations
• DTrace/SystemTap probing support
▫ Must compile using --with-dtrace
▫ Traces …
Function call/return
GC started/finished
Line of code executed
Stuff not from a PEP
16
Slide 17
Slide 17 text
Because people always like faster.
https://speed.python.org/
17
Slide 18
Slide 18 text
0%
20%
40%
60%
80%
100%
120%
2to3 Chameleon html5lib Tornado
speed.python.org results for "apps" benchmarks
on May 1 of in-development branches
(normalized to Python 2.7)
Python 2.7 Python 3.5 Python 3.6
18
Slide 19
Slide 19 text
If you have a project on Travis, please test your code against "3.6-dev"
19