Brought to you by Michael and Brian - take a Talk Python course or get Brian's pytest book

#131: Python 3 has issues (over on GitHub)

Published Tue, May 21, 2019, recorded Wed, May 15, 2019

Sponsored by DigitalOcean: pythonbytes.fm/digitalocean

Brian #1: PEP 581 (Using GitHub issues for CPython) is accepted

  • PEP 581
  • The email announcing the acceptance.
  • “The migration will be a large effort, with much planning, development, and testing, and we welcome volunteers who wish to help make it a reality. I look forward to your contributions on PEP 588 and the actual work of migrating issues to GitHub.” — Barry Warsaw

Michael #2: Replace Nested Conditional with Guard Clauses

  • Deeply nested code is problematic (does it have deodorant — err comments?)
  • But what can you do? Guard clauses!
  • See Martin Fowler’s article and this one.
        # BAD! 
        def checkout(user):
            shipping, express = [], []
            if user is not None:
                for item in user.cart:
                    if item.is_available:
                        shipping.append(item)
                        if item.express_selected:
                            express.append(item)
    
            return shipping, express
    
    # BETTER! 
    def checkout(user):
        shipping, express = [], []
        if user is None:
            return shipping, express

        for item in user.cart:
            if not item.is_available:
                continue

            shipping.append(item)
            if item.express_selected:
                express.append(item)

        return shipping, express

Brian #3: Things you’re probably not using in Python 3 – but should

  • Vinko Kodžoman
  • Some of course items:
    • f-strings
    • Pathlib (side note. pytest tmp_path fixture creates temporary directories and files with PathLib)
    • data classes
  • Some I’m warming to:
    • type hinting
  • And those I’m really glad for the reminder of:
  • enumerations

        from enum import Enum, auto
        class Monster(Enum):
            ZOMBIE = auto()
            WARRIOR = auto()
            BEAR = auto()
    
        print(Monster.ZOMBIE)
        # Monster.ZOMBIE
    
  • built in lru_cache: easy memoization with the functools.lru_cache decorator.

        @lru_cache(maxsize=512)
        def fib_memoization(number: int) -> int:
            ...
    
  • extended iterable unpacking

        >>> head, *body, tail = range(5)
        >>> print(head, body, tail)
        0 [1, 2, 3] 4
        >>> py, filename, *cmds = "python3.7 script.py -n 5 -l 15".split()
        >>> cmds
        ['-n', '5', '-l', '15']
        >>> first, _, third, *_ = range(10)
        >>> first, third
        (0, 2)
    

Michael #4: The Python Arcade Library

  • Arcade is an easy-to-learn Python library for creating 2D video games. It is ideal for people learning to program, or developers that want to code a 2D game without learning a complex framework.
  • Minesweeper games, hangman, platformer games in general.
  • Check out Sample Games Made With The Arcade Library too
  • Includes physics and other goodies
  • Based on OpenGL

Brian #5: Teaching a kid to code with Pygame Zero

  • Matt Layman
  • Scratch too far removed from coding.
  • Using Mu to simplify coding interface.
    • comes with a built in Python.
    • Pygame Zero preinstalled
  • [Pygame Zero] is intended for use in education, so that teachers can teach basic programming without needing to explain the Pygame API or write an event loop.”
  • Initial 29 line game taught:
    • naming things and variables
    • mutability and fiddling with “constants” to see the effect
    • functions and side effects
    • state and time
    • interactions and mouse events
  • Article also includes some tips on how to behave as the adult when working with kids and coding.

Michael #6: Follow up on GIL / PEP 554

  • Has the Python GIL been slain? by Anthony Shaw
  • multithreading in CPython is easy, but it’s not truly concurrent, and multiprocessing is concurrent but has a significant overhead.
  • Because Interpreter state contains the memory allocation arena, a collection of all pointers to Python objects (local and global), sub-interpreters in PEP 554 cannot access the global variables of other interpreters.
  • the way to share objects between interpreters would be to serialize them and use a form of IPC (network, disk or shared memory). All options are fairly inefficient
  • But: PEP 574 proposes a new pickle protocol (v5) which has support for allowing memory buffers to be handled separately from the rest of the pickle stream.
  • When? Pickle v5 and shared memory for multiprocessing will likely be Python 3.8 (October 2019) and sub-interpreters will be between 3.8 and 3.9.

Extras

Brian:

Michael:

Jokes

  • MK → Waiter: Would you like coffee or tea? Programmer: Yes.

Want to go deeper? Check our projects