Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Brett Cannon - What's new in Python 3.6

Brett Cannon - What's new in Python 3.6

Python 3.6 has turned out to be quite the release! With [16 Python Enhancement Proposals](https://www.python.org/dev/peps/pep-0494/) incorporated into the version, Python 3.6 is only surpassed by Python 3.0 for having more [PEPs](https://www.python.org/dev/peps/) included in a single release. This talk will be an overview of those 16 PEPs and other changes outlined in the [What's New](https://docs.python.org/3.6/whatsnew/3.6.html) document for Python 3.6.

https://us.pycon.org/2017/schedule/presentation/292/

PyCon 2017

May 21, 2017
Tweet

More Decks by PyCon 2017

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. • 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
  6. • 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
  7. 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
  8. 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
  9. • 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
  10. 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
  11. 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
  12. • 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
  13. • 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
  14. 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