Quick survey: locals in comprehensions (Python 3 only)

Tim Chase tim.chase at 1
Mon Jun 25 09:51:18 EDT 2018


From: Tim Chase <python.list at tim.thechases.com>

On 2018-06-24 05:03, Steven D'Aprano wrote:
> I'd like to run a quick survey. There is no right or wrong answer,
> since this is about your EXPECTATIONS, not what Python actually
> does.
>
> Given this function:
>
> def test():
>     a = 1
>     b = 2
>     result = [value for key, value in locals().items()]
>     return result
>
> what would you expect the result of calling test() to be?

I'd expect either [1,2] or [2,1] depending on whether it's py2 (where dict
iteration order isn't guaranteed, so could be either) or py3 (where dict order
is more stable/guaranteed)

> Is that the result you think is most useful?

While I have difficulty imagining a case in which I'd find this useful, if I
were writing this code, it's the "useful" result I'd expect.

> In your opinion, is this a useful feature, a misfeature, a bug, or
> "whatever"?

I'd file it in the "whatever" category, possibly useful to someone other than
me.

-tkc

--- BBBS/Li6 v4.10 Toy-3
 * Origin: Prism bbs (1:261/38)



More information about the Python-list mailing list