On Wed, 2 Dec 2015 12:16 pm, Erik wrote: > On 02/12/15 01:02, Steven D'Aprano wrote: >> On Tue, 1 Dec 2015 08:15 pm, Grobu wrote: >>> # ------------------------------------------------- >>> >>> def test(arg=[0]): >>> ... print arg[0] >>> ... arg[0] += 1 >> Awesome! > > Hideous! > >> using a mutable default as static storage. > > Exposing something a caller can override as a local, static, supposedly > "private" value is IMHO a tad ... ugly? (*)
Heh, I agree, and as I suggested, it might be good to have an actual mechanism for static locals. But using a class is no better: your "static storage" is exposed as an instance attribute, and even if you flag it private, *somebody* is going to mess with it. A closure works, but that obfuscates the code: def make_test(): arg = [0] def test(): print arg[0] arg[0] += 1 return test test = make_test() Or in Python 3: def make_test(): arg = 0 def test(): nonlocal arg print arg arg += 1 return test test = make_test() Python has three not-entirely-awful solutions to the problem of static locals, but no really great or obvious one. -- Steven -- https://mail.python.org/mailman/listinfo/python-list