Op 23-11-15 om 14:58 schreef Steven D'Aprano: > On Mon, 23 Nov 2015 09:40 pm, BartC wrote: > >> On 23/11/2015 07:47, Steven D'Aprano wrote: >> >>> I think it would be cleaner and better if Python had dedicated syntax for >>> declaring static local variables: >> >> Interesting. So why is it that when /I/ said: >> >> > On Mon, 23 Nov 2015 12:21 am, BartC wrote: >> > >> >> But if it's used for static storage, then why not just use static >> >> storage? >> >> You replied with the insulting: >> >> > /head-desk >> >> ? >> >> Maybe it's my turn to bang my head on the desk. > > Let me steal^W borrow an idea from Galileo, and present the explanation in > the form of a dialogue between two philosophers of computer science, > Salviati and Simplicio, and a third, intelligent layman, Sagredo. > > https://en.wikipedia.org/wiki/Dialogue_Concerning_the_Two_Chief_World_Systems > > > Salviati: Function defaults can also be used for static storage. > > Simplicio: If you want static storage, why not use static storage? > > Salviati: Function defaults in Python *are* static storage. > > Although they not the only way to get static storage, as closures can > also be used for that purpose, they are surely the simplest way to get > static storage in Python. > > Sagredo: Simplest though it might be, surely a reasonable person would > consider that using function parameters for static storage is abuse of the > feature and a global variable would be better? > > Salviati: Global variables have serious disadvantages. I will agree that > using function parameters for static storage is something of a code smell, > but good enough for rough and ready code. Nevertheless, it would be good if > Python had dedicated syntax for static storage. > > Simplicio: Ah-ha! Gotcha! > > Salviati: No, perhaps you missed that I was referring to a hypothetical > future addition to Python, not a current feature. But even if it did exist > today, your statement misses the point that by using function defaults I > *am* using static storage. In effect, you are telling me that rather than > using static storage I should instead use static storage.
If you really want mutable static storage, you can simulate it more cleanly with a closure than using a default argument. def factory(): static_dict = {} def myfunc(a, b): mutate static_dict as much as you like return myfunc myfunc = factory() Or you can use a class of course. -- https://mail.python.org/mailman/listinfo/python-list