static variables in Python?

Bruce Frederiksen is_this at visible.com
Tue Jul 29 21:23:00 EDT 2008


On Tue, 29 Jul 2008 21:31:01 +0000, kj wrote:

> In <w8CdnQGPtuvdGRLVnZ2dnUVZ_r7inZ2d at comcast.com> Larry Bates <larry.bates at websafe.com`> writes:
>
> [snip]
> 
> Maybe it's easier to see what I mean with JavaScript:
> 
> function foo() {
>   if (foo.x === undefined) foo.x = expensive_call();
>   return do_stuff_with(foo.x);
> }

def foo():
    if not hasattr(foo, 'x'): foo.x = expensive_call()
    return do_stuff_with(foo.x)

or, maybe just define foo in two steps:

def foo():
    return do_stuff_with(foo.x)

foo.x = expensive_call()



More information about the Python-list mailing list