[Python-ideas] As-do statements/anonymous blocks in python

James Lu jamtlu at gmail.com
Wed Jul 25 13:07:50 EDT 2018


I'm open to any changes or criticism.

```
import atexit
as atexit.register:
    # ...do various cleanup tasks...
    print('Goodbye')

# is approximately equivalent to =>
import atexit
def _():
    # ...do various cleanup tasks...
    print('Goodbye')
atexit.register(_)

# flask example
@app.route("/")
def hello():
    return "Hello World!"
# is approximately equivalent to =>
as app.route('/'):
    return "Hello World!"

@app.route('/user/<username>')
def show_user_profile(username):
    # show the user profile for that user
    return 'User %s' % username

as app.route('/user/<username>') do username:
    return "Hello World!"

def print_sorted(iterable, block):
    sorted(iterable, )

l = [1, 2, 3, 4, 'spam']

as l.sort(key=%) do obj:
    return str(obj)

# multiple arguments
as spam do a, b:
    ...
```
## `%`  function call syntax
Calling a function with a single percent in place of an argument creates a
new function.

```
lumberjack(15, %)
# is equivalent to the expression
lambda x: lumberjack(15, %)
```
Using `*` instead of `%` could also be possible.

```
import threading, time
def interval(seconds, block):
    def target():
        while True:
            time.sleep(seconds)
            if block():  # stop looping if block returns True
                break
    threading.Thread(target=target).start()

as interval(5, %):
   print("chirp")
# => chirp every 5 seconds on a seperate thread
as threading.Timer(5, %):
   print("hi")
# => say "hi" in 5 seconds
```

## `^` currying function definition syntax?
I'm not sure this is necessary or a good idea.
```
def interval(seconds, ^block):
    def target():
        while True:
            time.sleep(seconds)
            if block():  # stop looping if block returns True
                break
    threading.Thread(target=target).start()
# is aprroximately equivalent to
def interval(seconds, block=None):
    def inner(block):
        def target():
            while True:
                time.sleep(seconds)
                if block():
                    break
        threading.Thread(target=target).start()
    if block == None:
        def outer(block):
            return inner(block)
    else:
        return inner(block)

as interval(5):
    print('chirp')
# equivalent to
interval(5)(lambda: print('chirp'))
```
### Lazy evaluation of chained `%` calls?
This would allow things like:
```
start_on_new_thread = threading.Thread(target=%).start()
def bong():
    while True:
        time.sleep(6*60)
        print('BONG')
start_on_new_thread(bong)
# alternatively
as start_on_new_thread:
    while True:
        time.sleep(6*60)
        print('BONG')
```

## As-do statements in classes
```
class M():
     def __init__(self):
         self.time = 5
     as interval(self.time, %):
         print('chirp')
```

I'm not sure if this should be valid, and I'd like the community's input on
when as-do statements should be bound when as-do.

## Proposed Type Hinting Syntax
```
as app.route('/user/<username>') do (username: str):
    return "Hello World!"
```

Alternatives:
```
as app.route('/user/<username>') do username: str%
    return "Hello World!"
# like objective-c
as app.route('/user/<username>') do username: str^
    return "Hello World!"
# like coffeescript
as app.route('/user/<username>') do username: str ->
    return "Hello World!"
```

I’m not totally sure of practical uses, but I’m imagining it would make
passing a function to another function much more convenient. In React, you
pass an argument called `render` to a `FlatList`, and `render` renders an
item of the list. `FlatList` is a scrollable list that handles unloading
off-screen items and loading items that will appear in the scroll box soon.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180725/d35883cb/attachment-0001.html>


More information about the Python-ideas mailing list