Quick survey: locals in comprehensions (Python 3 only)

Antoon Pardon antoon.pardon at vub.be
Tue Jun 26 04:20:38 EDT 2018


On 24-06-18 07: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? Is that the 
> result you think is most useful? In your opinion, is this a useful 
> feature, a misfeature, a bug, or "whatever"?
>
> I'm only looking for answers for Python 3. (The results in Python 2 are 
> genuinely weird :-)

I would expect an UnboundLocalError: local variable 'result' referenced before assignment.

-- 
Antoon Pardon.




More information about the Python-list mailing list