Quick survey: locals in comprehensions (Python 3 only)

Tim Chase python.list at tim.thechases.com
Mon Jun 25 09:51:19 EDT 2018


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





More information about the Python-list mailing list