[Python-ideas] Default arguments in Python - the return
Pascal Chambon
chambon.pascal at wanadoo.fr
Sat May 9 13:16:46 CEST 2009
spir a écrit :
> Le Fri, 08 May 2009 22:31:57 +0200,
> Pascal Chambon <chambon.pascal at 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.append(n)
> squares.static_list = []
>
> squares(1);squares(2);squares(1);squares(3)
> print squares.static_list
>
> Denis
> ------
> la vita e estrany
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
>
>
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,
regards,
Pascal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20090509/e5d17960/attachment.htm>
More information about the Python-ideas
mailing list