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

Interesting new features in Python 3.5

Brett Cannon
September 13, 2015

Interesting new features in Python 3.5

Vancouver Python Day presentation on what Python 3.5 introduces.

Brett Cannon

September 13, 2015
Tweet

More Decks by Brett Cannon

Other Decks in Programming

Transcript

  1. Interesting New Features in Python 3.5 Dr. Brett Cannon Microsoft

    / snarky.ca Vancouver Python Day, Sep 2015
  2. "Coroutines … generalize subroutines for nonpreemptive multitasking, by allowing multiple

    entry points for suspending and resuming execution at certain locations."
  3. In the beginning, there were enhanced generators >>> def grep(look_for):

    ... while True: ... line = (yield) ... if look_for in line: print(line) ... >>> search = grep('Python'); next(search) # Primed >>> search.send('Hi, Ruby?') >>> search.send('Hi, Vancouver Python Day!') Hi, Vancouver Python Day!
  4. Then came asyncio @asyncio.coroutine def display_date(loop): end_time = loop.time() +

    5.0 while True: print(datetime.datetime.now()) if (loop.time() + 1.0) >= end_time: break yield from asyncio.sleep(1) loop = asyncio.get_event_loop() loop.run_until_complete(display_date(loop)); loop.close()
  5. Now we have async/await async def display_date(loop): end_time = loop.time()

    + 5.0 while True: print(datetime.datetime.now()) if (loop.time() + 1.0) >= end_time: break await asyncio.sleep(1) loop = asyncio.get_event_loop() loop.run_until_complete(display_date(loop)); loop.close()
  6. Take a deep breath & stay calm • Does not

    affect runtime semantics • Meant to act as documentation and help with tooling ◦ Just like Dart • Does not forbid using function annotations for other purposes ◦ Does gives them widely recognized default semantics, though • In no way required to be used • Doesn't expect everything to be annotated ◦ E.g., don't worry about annotating local variables if you don't want to
  7. Example using the typing module and variable annotations from typing

    import Mapping, Sequence def notify_by_email(employees: Sequence[Employee], overrides: Mapping[str, str]) -> None: ... x = [] # type: List[Employee]
  8. Prints "I'm in Vancouver learning about Python!" loc = 'Vancouver'

    lang = 'Python' print(f"I'm in {loc} learning about {lang}!")
  9. Q&A