static keyword
Michael Walter
cm at leetspeak.org
Fri Apr 30 08:28:03 EDT 2004
Peter Hansen wrote:
> Nick Jacobson wrote:
>
>> I believe the following "static" command would be useful in Python.
>
> [snip]
>
>> Just like in C, the variables i and firstcall are only assigned the
>> first time foo() is called. To get this effect currently, one could
>> use default arguments or wrapping the whole thing in a class. Both of
>> these solutions seem like hacks, the above method IMO is more
>> Pythonic. :)
>
>
> I'm not sure how to interpret the smiley, but I'll take it
> you weren't actually joking...
>
> Why do you call using OO ("wrapping it in a class", as you say)
> a "hack"? Generally speaking, using objects to contain state
> information such as this is exactly what most people would call
> the cleanest, best approach.
> [...]
There's also a bunch of people who would consider this (modulo my
obvious mistakes ;) the cleanest, best approach:
(let ((first-call #t)
(i '(10 11)))
(define (foo)
(when (first-call)
(begin (display "first pass") (set! first-call #f))
(set! (array-ref i 0) (+ (array-ref i 0) 1))
(display (array-ref i 0))))
I.e. capture the state in a normal lexical variable.
Cheers,
Michael
More information about the Python-list
mailing list