static variables

Steven D'Aprano steve at pearwood.info
Tue Dec 1 20:02:31 EST 2015


On Tue, 1 Dec 2015 08:15 pm, Grobu wrote:
> Perhaps you could use a parameter's default value to implement your
> static variable?
>> Like :
> # -------------------------------------------------
> >>> def test(arg=[0]):
> ... print arg[0]
> ... arg[0] += 1
> ...

Awesome!
I'm not being sarcastic, I'm serious. Thank you Grobu, for demonstrating the
existence of something we debated a few days ago.
In another *painfully* long thread about mutable default arguments, I
suggested the same idea: using a mutable default as static storage. It is
very gratifying to see that other people have independently come up with
the same idea.
-- 
Steven


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /