On Mon, 5 Dec 2022 at 08:34, Bruce Leban <[email protected]> wrote:
>
>
> I can put that in a function which still leaves the function in scope:
>
> def __create_thing():
> part1 = (some calculation)
> part2 = (some other calculation)
> return combine(part1, part2)
> THING = __create_thing()
>
> If we had a top-level-only local statement, I might use it but note that it's
> still clumsy since I have to make THING non-local:
>
> local:
> part1 = (some calculation)
> part2 = (some other calculation)
> nonlocal THING
> THING = combine(part1, part2)
>
# put this in your site.py or whatever so it's always available
def scope(f): return f()
# then do this
@scope
def THING():
part1 = (some calculation)
part2 = (some other calculation)
return combine(part1, part2)
It's better than multiline lambda, and has all the benefits of a
nested scope. It doesn't leave the function lying around - it reuses
the name for the value the function returns.
ChrisA
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/TRJ3HZSFBZXZV365BN5HPOH6N2EWANVY/
Code of Conduct: http://python.org/psf/codeofconduct/