Search

Home > Python Bytes > #171 Chilled out Python decorators with PEP 614
Podcast: Python Bytes
Episode:

#171 Chilled out Python decorators with PEP 614

Category: Technology
Duration: 00:34:34
Publish Date: 2020-03-05 02:00:00
Description:

Sponsored by Datadog: pythonbytes.fm/datadog

Special guest: David Amos

David #1: PEP 614 – Relaxing Grammar Restrictions on Decorators

  • Python currently requires that all decorators consist of a dotted name, optionally followed by a single call.
    • E.g., can’t use subscripts or chained calls
  • PEP proposes allowing any valid expression.
  • Motivation for limitation is not a technical requirement:
    • “I have a gut feeling about this one. I'm not sure where it comes from, but I have it... So while it would be quite easy to change the syntax in the future, I'd like to stick to the more restricted form unless a real use case is presented where [changing the syntax] would increase readability.”
    • (Guido van Rossom, Source)
  • Use case highlighted by PEP:
    • List of Qt buttons: buttons = [button0, button1, …]
    • Decorator is a method on a class attribute: button.clicked.connect
    • Under current restrictions you can’t do @button[0].clicked.connect
    • Workarounds involve assigning list element to a variable first:
      • button0 = buttons[0]
      • @button0.clicked.connect
  • Author points out grammar is already loose enough to hack around:
    • Define function def _(x): return x
    • Then use _ as your decorator: @_(buttons[0].clicked.connect)
    • That’s less readable than just using the subscript
  • PEP proposes relaxing grammar to “any valid expression” (sort of), i.e. anything that you can use as a test in if, elif, or while blocks (as opposed to valid string input to eval)
    • Some things wouldn’t be allowed, though
    • E.g., tuples require parentheses, @f, g doesn’t make sense
    • Does a tuple as a decorator make sense in the first place, though?
  • CPython implementation on GitHub:

Michael #2: Create a macOS Menu Bar App with Python (Pomodoro Timer)

  • by Camillo Visini
  • Nice article: Learn how to create your very own macOS Menu Bar App using Python, rumps and py2app
  • The mac menu bar is super useful. I leverage the heck out of this thing. Why not write Python for it?
  • Tools:
    • Python 3 and PyCharm as an IDE
    • Rumps → Ridiculously Uncomplicated macOS Python Statusbar apps
    • py2app → For creating standalone macOS apps from Python code (how cool is that?)
  • Get started with the code:
    app = rumps.App("Pomodoro", "                                
Total Play: 0

Some more Podcasts by Michael Kennedy

500+ Episodes
Talk Python .. 300+     50+