spir a écrit :
Le Fri, 08 May 2009 22:31:57 +0200,
Pascal Chambon <chambon.pascal@wanadoo.fr> s'exprima ainsi:

And no one seemed to enjoy the possibilities of getting "potentially 
static variables" this way. Static variables are imo a rather bad idea, 
since they create "stateful functions", that make debugging and 
maintenance more difficult ; but  when such static variable are, 
furthermore, potentially non-static (i.e when the corresponding function 
argument is supplied), I guess they become totally useless and dangerous 
- a perfect way to get hard-to-debug behaviours.

If we want static vars, there are better places than default args for this. (See also the thread about memoizing). E.g. on the object when it's a method, or even on the func itself.

def squares(n):
   square = n * n; print square
   if square not in squares.static_list:
squares.static_list = []

print squares.static_list

la vita e estrany
Python-ideas mailing list

Well, I've just realized I'd sent a semi-dumb question in my previous answer :p

I'd never quite realized it was possible to store stuffs inside the function object, by retrieving it from inside the code object.

And it works pretty well...
In classes you can access your function via self, in closures they get caught in cells... it's only in global scope that there are problems : here, if you rename squares (newsquares = squares ; squares = None), you'll have an error by calling newsquares, because it searches "squares" in the global scope, without the help of "self" or closures.

Still, an explicit way of targetting "the function I'm in" would be sweet imo, but retrieving it that way is not far from being as handy.

Thanks for the tip that opened my eyes,