Cool New Features in Python 3.5

Cool New Features in Python 3.5

B3b78ba1acbf09ba202987d2d93ab72f?s=128

Kevin McCarthy

November 07, 2015
Tweet

Transcript

  1. Cool new features in Python 3.5

  2. None
  3. Unpacking >>> a,b,c = [1,2,3] >>> print(a) 1 >>> print(b)

    2 >>> print(c) 3
  4. Unpacking: LEVELED >>> a = [1,2,3] >>> b = [4,5,6]

    >>> print([*a, *b]) [1,2,3,4,5,6] PEP 448
  5. Build Dictionaries >>> defaults = {"database": "mysql"} >>> my_config =

    {"database": "sqlite", "password": "cats"} PEP 448
  6. Build Dictionaries >>> defaults.update(my_config) >>> print defaults {"database": "sqlite", "password":

    "cats"} PEP 448
  7. Build Dictionaries >>> print {**defaults, **my_config} {"database": "sqlite", "password": "cats"}

    PEP 448
  8. Call Functions with Unpacking >>> connect(**defaults, **my_config) PEP 448

  9. math.isclose() >>> a = 1.0 >>> b = 1.00000000001 >>>

    math.isclose(a,b) True >>> math.isclose(1.0, 1.1) False >>> math.isclose(1.0, 1.01, abs_tol=.1) True PEP 485
  10. >>> import math >>> math.pow(a, 2) + math.pow(b, 2) ==

    math.pow(c,2)
  11. >>> a**2 + b**2 == c**2

  12. import numpy as np from numpy.linalg import inv, solve #

    Using dot function: S = np.dot((np.dot(H, beta) - r).T, np.dot(inv(np.dot(np.dot(H, V), H.T)), np.dot(H, beta) - r))
  13. @ matrix multiplication operator @ >>> S = (H @

    beta - r).T @ \ inv(H @ V @ H.T) @ (H @ beta - r) PEP 465
  14. os.scandir() PEP 471

  15. yield from (python 3.4) @asyncio.coroutine def view(request): data = yield

    from db_query(request.q) more = yield from db_query(request.p) return yield from render(data, more) PEP 380
  16. async and await async def view(request): data = await db_query(request.q)

    more = await db_query(request.p) return await render(data, more) PEP 492
  17. type annotations def hello(name: str) -> str: return "hello, {}".format(name)

    PEP 484
  18. type annotations def hello(name: str) -> str: return "hello, {}".format(name)

    def greet(name): print greeting(name) PEP 484 types!
  19. gradual typing def hello(name: str) -> str: return "hello, {}".format(name)

    def greet(name): print(greeting(name)) types! no types! PEP 484
  20. import typing from typing import List def combine(input: List[str]) ->

    int: return input.join() PEP 484
  21. much, much more! PEP 461, 479… • subprocess.run() • collections.OrderedDict

    4-100 times faster • bytes % args, bytearray % args • functools.lru_cache() 10-25x faster • StopIteration handling inside generators better • A new installer for Windows
  22. Thanks! https://docs.python.org/3/whatsnew/3.5.html