Static variables [was Re: syntax difference]

Chris Angelico chris.angelico at 1
Sun Jun 24 10:42:12 EDT 2018


From: Chris Angelico <rosuav at gmail.com>

On Sun, Jun 24, 2018 at 9:37 AM, Bart <bc at freeuk.com> wrote:
> On 23/06/2018 23:25, Ben Bacarisse wrote:
>>
>> Bart <bc at freeuk.com> writes:
>>
>>> On 23/06/2018 21:13, Chris Angelico wrote:
>>>>
>>>> On Sat, Jun 23, 2018 at 10:41 PM, Bart <bc at freeuk.com> wrote:
>>>
>>>
>>>>> (At what point would that happen anyway; if you do this:
>>>
>>>
>>>> NONE of your examples are taking copies of the function. They all are
>>>> making REFERENCES to the same function. That is all.
>>>
>>>
>>> This is about your notion that invocations of the same function via
>>> different references, should maintain their own versions of the
>>> function's 'static' data.
>>>
>>> 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?
>>>
>>> What is different, what extra bit of information is provided when f()
>>> is invoked via i() or j()?
>>
>>
>> f is not being invoked by either i() or j().  i = f() binds i to the
>> function returned by f.  That's a newly minted function.  In languages
>> like Python, executing def creates a function.
>
>
> Do you mean that if the same 'def' block is re-executed, it will create a
> different instance of the function? (Same byte-code, but a different set of
> everything else the function uses.)
>
> Wow. (Just think of all the times you write a function containing a neat
> bunch of local functions, every time it's called it has to create a new
> function instances for each of those functions, even if they are not used.)

... do you even understand what closures are?

ChrisA

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



More information about the Python-list mailing list