Quick survey: locals in comprehensions (Python 3 only)
Ben Finney
ben.finney at 1
Mon Jun 25 10:33:34 EDT 2018
From: Ben Finney <ben+python at benfinney.id.au>
Paul Moore <p.f.moore at gmail.com> writes:
> On 24 June 2018 at 06:03, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
> > 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'm aware of the background for this question. Is there any equivalent
> question that doesn't use locals()? The reason I ask is that I see
> locals() as "digging into implementation stuff" and sort of expect it
> to act oddly in situations like this...
My understanding of Steven's question is to give an unambiguous way to:
* Express the question Γ úwhich name bindings do you expect to exist in
this local function scope, by the time of the Γ returnΓ ╓ statement?Γ ╪.
* Avoid prejudicing the reader to expect any particular binding to be
active.
One way to do the first, at the cost of losing the second, might be this::
def test():
a = 1
b = 2
[value for key, value in dict().items()]
print(a)
print(b)
print(key)
print(value)
and then ask Γ úWhich of those statements do you expect to fail with
NameError?Γ ╪.
But I may have misunderstood some nuance of what is being asked, which is to be
expected because Steven was deliberately trying to avoid having the reader
second-guess what the purpose of the code is.
--
\ Γ úI wish there was a knob on the TV to turn up the intelligence. |
`\ There's a knob called Γ brightnessΓ ╓ but it doesn't work.Γ ╪ |
_o__) Γ ÷Eugene P. Gallagher |
Ben Finney
--- BBBS/Li6 v4.10 Toy-3
* Origin: Prism bbs (1:261/38)
More information about the Python-list
mailing list