Global VS Local Subroutines

BlindAnagram blindanagram at nowhere.org
Thu Feb 10 11:15:00 EST 2022


On 10/02/2022 15:20, Chris Angelico wrote:
> 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

Thanks Chris, I thought it  was mostly a design choice but I wasn't sure 
whether there would be any significant performance issues.

    Brian


More information about the Python-list mailing list