On Mon, 5 Dec 2022 at 08:34, Bruce Leban <br...@leban.us> 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 -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/TRJ3HZSFBZXZV365BN5HPOH6N2EWANVY/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to