Static variables [was Re: syntax difference]

Steven D'Aprano steven.d'aprano at 1
Sun Jun 24 03:23:36 EDT 2018


From: Steven D'Aprano <steve+comp.lang.python at pearwood.info>

On Sat, 23 Jun 2018 21:44:00 +0100, Bart wrote:

> Since these references are created via the return g statement here:
>
>      def f():
>          def g():
>              ....
>          return g
>
> (say to create function references i and j like this:
>
>      i = f()
>      j = f()
> )
>
> I'm assuming that something special must be happening. Otherwise, how
> does f() know which reference it's being called via?

You assume wrong.


> What is different, what extra bit of information is provided when f() is
> invoked via i() or j()?

For somebody who has been using Python for a few years now, and is constantly
telling us how we're doing it wrong, you sure are ignorant about the language
and how it works.

For somebody who has been programming for so long, you seem awfully unaware of
how dynamic languages with first-class functions and closures work.

No extra bit of information is provided. i refers to one function, j, refers to
 another function, and there is no overlap.

Each invocation of f() results in a brand new function being created,
dynamically, and assigned to the appropriate name.



--
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing it everywhere."
 -- Jon Ronson

--- BBBS/Li6 v4.10 Toy-3
 * Origin: Prism bbs (1:261/38)



More information about the Python-list mailing list