Global VS Local Subroutines
Chris Angelico
rosuav at gmail.com
Thu Feb 10 10:20:33 EST 2022
On Fri, 11 Feb 2022 at 02:13, BlindAnagram <blindanagram at nowhere.org> wrote:
>
> Is there any difference in performance between these two program layouts:
>
> def a():
> ...
> def(b):
> c = a(b)
>
> or
>
> def(b):
> def a():
> ...
> c = a(b)
>
> I would appreciate any insights on which layout to choose in which
> circumstances.
>
Great question! The difference is that, in the second case, a() isn't
available anywhere else. So the real question is: Is that good or bad?
Does it make sense to call a() from outside of your second function,
even for testing? If so, maybe name it _a() so it's private, but keep
it global. Does a() need a lot of information from the context of your
second function? If so, it's easier to have a closure, with one
function inside another.
Both styles make sense, and your question is well put: it's a design
decision with no "right" or "wrong", just different choices with
different implications.
ChrisA
More information about the Python-list
mailing list