
Steven D'Aprano wrote:
That's a definition of "within" that I'm not really happy with. And I think it's wrong. Consider the timing of calls:
To my mind, to say that the contents of the where-block occur "within" the call to sort, it would imply that you get output like this:
Call to sort() started at 1234560000.23 seconds Enter the where-block at 1234560000.24 seconds Call to sort() ended at 1234560555.25 seconds
By that definition though, a lambda isn't compiled "within" the sort expression. First the lambda is compiled then it is passed as an argument to sort. But you earlier wrote that,
For the function definition to appear inside the call to sort, you'd need to write something like:
items.sort(key=lambda item: value)
But that doesn't follow at all. Items.sort isn't called until there are arguments that can be passed to it. The timing of this would be the exact same as a where block:
import time class MyList(list): ... def sort(self, *args, **kwargs): ... print("Call to sort() started at %s seconds" % time.time()) ... super(MyList, self).sort(*args, **kwargs) ... print("Call to sort() ended at %s seconds" % time.time()) ... items = MyList([-9, 7, -4, 2, -1, 0, 3])
def do_stuff(): ... print("Enter the fake where-block at %s seconds" % time.time()) ... time.sleep(10) ... items.sort(key=lambda item, default=do_stuff(): abs(item)) Enter the where-block at 1247970018.89 seconds Call to sort() started at 1247970028.89 seconds Call to sort() ended at 1247970028.89 seconds
-- Carl