Static variables [was Re: syntax difference]

Grant Edwards grant.b.edwards at gmail.com
Mon Jun 25 14:22:56 EDT 2018


On 2018-06-24, Steven D'Aprano <steven.d'aprano at 1> wrote:

> Building functions is cheap. Cheap is not free.
>
> Inner functions that aren't exposed to the outside cannot be tested
> in isolation, you can't access them through help()
> interactively. Given the choice between:

[...]

> so not expensive, but not free either. If using an inner function
> has no advantages to you, why pay that tiny cost for no benefit?

The benefit of an inner function is that it makes it perfectly clear
to the reader that that function is not used outside the function
where it is defined.  It also makes it more difficult for other
fuctions to muck things up by changing the binding of a global name.

IOW, you use a local function instead of a global one for the exact
same reasons you use local "variables" instead of global ones.

In Python, functions are first class objects.  Binding a name to a
function is no different than binding it to an integer, list, string,
or dict.  Don't the global vs. local cost vs. benefit calculations
apply equally well to function objects as they do to those other sorts
of objects?

-- 
Grant Edwards               grant.b.edwards        Yow! I'm a nuclear
                                  at               submarine under the
                              gmail.com            polar ice cap and I need
                                                   a Kleenex!




More information about the Python-list mailing list