[Python-ideas] Re: [Feature] Body closure

2020-07-13 Thread Alexandre Brault

On 2020-07-13 5:12 p.m., Joao S. O. Bueno wrote:

You know you can simply pass functions as parameters, right?
def lock(func, *args):
   # ...setup
   result = func(*args)
   #... teardown
  return result.

And then, with 3 more lines you do a decorator out of that

def locked(func):
    # <'lock' body as above>
    return lock


in the end:

@locked
def mythings():
   # code that runs 'locked'
   ...


On Mon, 13 Jul 2020 at 17:48, > wrote:


Today I think about lambda in Python and what if we introduce the
new syntax:
```python
def lock(*args, closure):
    # Do some stuff
    closure() # Call closure
    # Finish stuff

if __name__ == '__main__':
    lock():
        # Do some things here is thread safe
```

This feature could be very similar as in `Kotlin` inline functions
https://kotlinlang.org/docs/reference/inline-functions.html


Or even better, a context manager

from contextlib import contextmanager
@contextmanager
def lock(*args):
    # Do some stuff
    yield
    # Finish stuff

if __name__ == '__main__':
    with lock():
    # Do somethings here is thread safe

It's even so interesting that I don't even need to implement the lock 
function; there's already the Lock class in the threading module


___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/QVSVP4YFGAQCU55364WYD2FLFIDZAZS6/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: [Feature] Body closure

2020-07-13 Thread Joao S. O. Bueno
You know you can simply pass functions as parameters, right?
def lock(func, *args):
   # ...setup
   result = func(*args)
   #... teardown
  return result.

And then, with 3 more lines you do a decorator out of that

def locked(func):
# <'lock' body as above>
return lock


in the end:

@locked
def mythings():
   # code that runs 'locked'
   ...


On Mon, 13 Jul 2020 at 17:48,  wrote:

> Today I think about lambda in Python and what if we introduce the new
> syntax:
> ```python
> def lock(*args, closure):
> # Do some stuff
> closure() # Call closure
> # Finish stuff
>
> if __name__ == '__main__':
> lock():
> # Do some things here is thread safe
> ```
>
> This feature could be very similar as in `Kotlin` inline functions
> https://kotlinlang.org/docs/reference/inline-functions.html
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/HAIFGFE5YUCGLEWFPT3K2N3H6WBK4KQ7/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/QTLUKWVALORYHCS23SUHN4GSEAVOGHZS/
Code of Conduct: http://python.org/psf/codeofconduct/