CPython 3.6.4:
from functools import partial
def add(x, y, z, a):
return x + y + z + a
list_of_as = list(range(10000))
def max1():
return max(list_of_as , key=lambda a: add(10, 20, 30, a))
def max2():
return max(list_of_as , key=partial(add, 10, 20, 30))
Run Code Online (Sandbox Code Playgroud)
现在:
In [2]: %timeit max1()
4.36 ms ± 42.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [3]: %timeit max2()
3.67 ms ± 25.9 µs per loop (mean ± std. dev. of …
Run Code Online (Sandbox Code Playgroud) 在Python中,您可以使用这样的with
语句(源代码):
class controlled_execution:
def __enter__(self):
# set things up
return thing
def __exit__(self, type, value, traceback):
# tear things down
with controlled_execution() as thing:
# some code
Run Code Online (Sandbox Code Playgroud)
在Flask/Jinja2中,使用flash消息的标准过程如下(源):
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
<!-- do stuff with `message` -->
{% endfor %}
{% endif %}
{% endwith %}
Run Code Online (Sandbox Code Playgroud)
我想知道{% with messages = get_flashed_messages() %}
语法方面的工作原理.
我无法在纯Python中重新创建它:
with messages = get_flashed_messages(): pass
加薪 …