Re: Function to avoid a global variable

2020-05-01 Thread DL Neil via Python-list
On 2/05/20 12:00 PM, Bob van der Poel wrote: I still think that the use of a keyword like "static" would be easiest. def foo(arg): static counter = 0 counter += 1 if counter ... And in this case static just means that it's a variable only readable inside foo() and it should

Re: Function to avoid a global variable

2020-05-01 Thread DL Neil via Python-list
On 2/05/20 11:30 AM, Chris Angelico wrote: On Sat, May 2, 2020 at 9:14 AM DL Neil via Python-list wrote: On 28/04/20 7:36 PM, Chris Angelico wrote: "Best"? Not sure about that. Functions are first-class objects in Python, so a function *is* a callable object. You don't have to create a

Re: Function to avoid a global variable

2020-05-01 Thread Bob van der Poel
I still think that the use of a keyword like "static" would be easiest. def foo(arg): static counter = 0 counter += 1 if counter ... And in this case static just means that it's a variable only readable inside foo() and it should maintain it's value between calls. A "global" without

Re: Function to avoid a global variable

2020-05-01 Thread Chris Angelico
On Sat, May 2, 2020 at 9:14 AM DL Neil via Python-list wrote: > > On 28/04/20 7:36 PM, Chris Angelico wrote: > >>> "Best"? Not sure about that. Functions are first-class objects in > >>> Python, so a function *is* a callable object. You don't have to create > >>> a custom class with a call method

Re: Function to avoid a global variable

2020-05-01 Thread DL Neil via Python-list
On 28/04/20 7:36 PM, Chris Angelico wrote: "Best"? Not sure about that. Functions are first-class objects in Python, so a function *is* a callable object. You don't have to create a custom class with a call method just to be able to attach attributes to your function. ChrisA Using a mutable

Re: Function to avoid a global variable

2020-04-29 Thread Tony Flury via Python-list
On 28/04/2020 06:49, jf...@ms4.hinet.net wrote: bvdp於 2020年4月28日星期二 UTC+8上午9時46分35秒寫道: Oh my, that is very cool! So, I can do this: def foo(i): if not 'bar' in foo.__dict__: foo.bar = 5 foo.bar += i You can have function attribute created this way if you like: def

Re: Function to avoid a global variable

2020-04-28 Thread Christian Gollwitzer
Am 28.04.20 um 09:54 schrieb ast: funny ! So we found 4 different ways to handle a memory in a function 1- Use a function parameter with a mutable default value 2- Use a function attribute 3- Use a callable object, and store your stuff inside an object attr 4- Use a closure to emulate a

Re: Function to avoid a global variable

2020-04-28 Thread ast
Le 28/04/2020 à 09:52, ast a écrit : Le 28/04/2020 à 09:39, Antoon Pardon a écrit : Op 27/04/20 om 18:39 schreef Bob van der Poel: Thanks Chris! At least my code isn't (quite!) as bad as the xkcd example :) Guess my "concern" is using the initialized array in the function:     def

Re: Function to avoid a global variable

2020-04-28 Thread ast
Le 28/04/2020 à 09:39, Antoon Pardon a écrit : Op 27/04/20 om 18:39 schreef Bob van der Poel: Thanks Chris! At least my code isn't (quite!) as bad as the xkcd example :) Guess my "concern" is using the initialized array in the function:     def myfunct(a, b, c=array[0,1,2,3] ) always

Re: Function to avoid a global variable

2020-04-28 Thread Antoon Pardon
Op 27/04/20 om 18:39 schreef Bob van der Poel: Thanks Chris! At least my code isn't (quite!) as bad as the xkcd example :) Guess my "concern" is using the initialized array in the function: def myfunct(a, b, c=array[0,1,2,3] ) always feels like an abuse. Has anyone seriously

Re: Function to avoid a global variable

2020-04-28 Thread Chris Angelico
On Tue, Apr 28, 2020 at 5:26 PM ast wrote: > > Le 28/04/2020 à 09:13, Chris Angelico a écrit : > > On Tue, Apr 28, 2020 at 4:56 PM ast wrote: > >> > >> Le 27/04/2020 à 04:46, Bob van der Poel a écrit : > > > > > > "Best"? Not sure about that. Functions are first-class objects in > > Python, so a

Re: Function to avoid a global variable

2020-04-28 Thread ast
Le 28/04/2020 à 09:13, Chris Angelico a écrit : On Tue, Apr 28, 2020 at 4:56 PM ast wrote: Le 27/04/2020 à 04:46, Bob van der Poel a écrit : "Best"? Not sure about that. Functions are first-class objects in Python, so a function *is* a callable object. You don't have to create a custom

Re: Function to avoid a global variable

2020-04-28 Thread Chris Angelico
On Tue, Apr 28, 2020 at 4:56 PM ast wrote: > > Le 27/04/2020 à 04:46, Bob van der Poel a écrit : > > Does this make as much sense as anything else? I need to track calls to a > > function to make sure it doesn't get called to too great a depth. I had a > > global which I inc/dec and then check in

Re: Function to avoid a global variable

2020-04-28 Thread ast
Le 28/04/2020 à 08:51, ast a écrit : Le 27/04/2020 à 04:46, Bob van der Poel a écrit : Does this make as much sense as anything else? I need to track calls to a function to make sure it doesn't get called to too great a depth. I had a global which I inc/dec and then check in the function. Works

Re: Function to avoid a global variable

2020-04-28 Thread ast
Le 27/04/2020 à 04:46, Bob van der Poel a écrit : Does this make as much sense as anything else? I need to track calls to a function to make sure it doesn't get called to too great a depth. I had a global which I inc/dec and then check in the function. Works fine, but I do need to keep a global

Re: Function to avoid a global variable

2020-04-27 Thread jfong
bvdp於 2020年4月28日星期二 UTC+8上午9時46分35秒寫道: > Oh my, that is very cool! So, I can do this: > > def foo(i): > if not 'bar' in foo.__dict__: > foo.bar = 5 > foo.bar += i You can have function attribute created this way if you like: def foo(i): foo.bar += i foo.bar = 5

Re: Function to avoid a global variable

2020-04-27 Thread Bob van der Poel
Oh my, that is very cool! So, I can do this: def foo(i): if not 'bar' in foo.__dict__: foo.bar = 5 foo.bar += i for a in range(10): foo(1) print (foo.bar) Thanks. I will have to play more with this. On Mon, Apr 27, 2020 at 5:31 PM Michael Torrie wrote: > On 4/27/20

Re: Function to avoid a global variable

2020-04-27 Thread Michael Torrie
On 4/27/20 10:39 AM, Bob van der Poel wrote: > Thanks Chris! > > At least my code isn't (quite!) as bad as the xkcd example :) > > Guess my "concern" is using the initialized array in the function: > >def myfunct(a, b, c=array[0,1,2,3] ) > > always feels like an abuse. > > Has anyone

Re: Function to avoid a global variable

2020-04-27 Thread Bob van der Poel
Thanks Chris! At least my code isn't (quite!) as bad as the xkcd example :) Guess my "concern" is using the initialized array in the function: def myfunct(a, b, c=array[0,1,2,3] ) always feels like an abuse. Has anyone seriously considered implementing a true static variable in a

Re: Function to avoid a global variable

2020-04-26 Thread Chris Angelico
On Mon, Apr 27, 2020 at 1:39 PM Bob van der Poel wrote: > > Does this make as much sense as anything else? I need to track calls to a > function to make sure it doesn't get called to too great a depth. I had a > global which I inc/dec and then check in the function. Works fine, but I do > need to

Function to avoid a global variable

2020-04-26 Thread Bob van der Poel
Does this make as much sense as anything else? I need to track calls to a function to make sure it doesn't get called to too great a depth. I had a global which I inc/dec and then check in the function. Works fine, but I do need to keep a global around just for this. So ... instead I wrote a