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, <[email protected]
<mailto:[email protected]>> 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 -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/QVSVP4YFGAQCU55364WYD2FLFIDZAZS6/
Code of Conduct: http://python.org/psf/codeofconduct/