static variables in Python?

5lvqbwl02 at sneakemail.com 5lvqbwl02 at sneakemail.com
Wed Jul 30 16:05:24 EDT 2008




kj wrote:
> Yet another noob question...
>
> Is there a way to mimic C's static variables in Python?  Or something
> like it?  The idea is to equip a given function with a set of
> constants that belong only to it, so as not to clutter the global
> namespace with variables that are not needed elsewhere.
>
> For example, in Perl one can define a function foo like this
>
> *foo = do {
>   my $x = expensive_call();
>   sub {
>     return do_stuff_with( $x, @_ );
>   }
> };
>
> In this case, foo is defined by assigning to it a closure that has
> an associated variable, $x, in its scope.
>
> Is there an equivalent in Python?
>



I found the following link addressing this problem several different
ways.  My favorite (trickiest) way is using decorators...

http://www.daniweb.com/code/snippet501.html




More information about the Python-list mailing list