Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python proposals

Python Proposals

cast decorator

The common python pattern

def foo():
    tmp = []
    for x in y:
        ...
        tmp.append(z)
    return tmp

can be simplified to

@cast
def foo() -> list:
    for x in y:
        ...
        yield z

using the decorator cast, which I have implemented here.

Some examples:

@cast
def nums() -> dict:
    yield 1, 'one'
    yield 2, 'two'
# {1: 'one', 2: 'two'}

@cast
def hello() -> (str.upper, ', '.join):
    yield "Hello"
    yield "world"
# 'HELLO, WORLD'

A decorator for function composition as follows would be useful.

compose = lambda f: lambda g: lambda *a, **k: f(g(*a, **k))

@compose(str.upper)
@compose(', '.join)
def hello():
    yield "Hello"
    yield "world"

However, the notation achieved by cast is superiour.

continue for coroutines

We can nicely loop through generators using for loops. However, for coroutines, this is not possible, since we have no way of sending values. I therefore suggest allowing continue to take an optional expression to send to the generator. For example, in

for x in g:
    ...
    continue y

the first value of x would be obtained from next(g) as usual, but subsequent values are obtained from g.send(y) (of course, allowing continue to be used in branches as it is currently, with g.send(None) or next(g) used if an iteration ends without a continue).

Currently this has to be done using the more clumsy

x = next(g)
try:
    while True:
        ...
        x = g.send(y)
except StopIteration:
    pass

else assertion

else EXPRESSION:
    ...

as shorthand for

else:
    assert(EXPRESSION)
    ...

though perhaps a new exception type should be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.