Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

static variables in Python?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


socyl at 987jk

Jul 29, 2008, 1:40 PM

Post #1 of 13 (357 views)
Permalink
static variables in Python?

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?

Thanks!

kynn
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
--
http://mail.python.org/mailman/listinfo/python-list


larry.bates at websafe

Jul 29, 2008, 1:57 PM

Post #2 of 13 (346 views)
Permalink
Re: static variables in Python? [In reply to]

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?
>
> Thanks!
>
> kynn


First names in Python are just that, names that point to objects. Those objects
can contain any type of information including other objects. They are NOT
buckets where things are stored.

1) Names (variables in Perl/C) defined within a Python function are placed in
its local namespace. They are not visible in the global namespace.

2) Yes you can have a local name point to a global. This is often used in
classes with attributes because looking up local is somewhat quicker than
looking up the class attribute.

def foo():
x = expensive_call
return do_stuff_with(x())

In this particular case it doesn't really help.

It would be more useful in something like:

class foo(object):
def __init__(self, initialvalue = 0)
self.currentvalue = initialvalue

def longloopingmethod(self, listtosum):
currentvalue = self.currentvalue
for v in listtosum:
currentvalue += v


BTW - There are BETTER ways to sum a list, so this is just an example.

-Larry
--
http://mail.python.org/mailman/listinfo/python-list


socyl at 987jk

Jul 29, 2008, 2:31 PM

Post #3 of 13 (341 views)
Permalink
Re: static variables in Python? [In reply to]

In <w8CdnQGPtuvdGRLVnZ2dnUVZ_r7inZ2d [at] comcast> Larry Bates <larry.bates [at] websafe`> writes:

>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?
>>
>> Thanks!
>>
>> kynn


>First names in Python are just that, names that point to objects. Those objects
>can contain any type of information including other objects. They are NOT
>buckets where things are stored.

>1) Names (variables in Perl/C) defined within a Python function are placed in
>its local namespace. They are not visible in the global namespace.

>2) Yes you can have a local name point to a global. This is often used in
>classes with attributes because looking up local is somewhat quicker than
>looking up the class attribute.

>def foo():
> x = expensive_call
> return do_stuff_with(x())

Maybe I'm missing your point, the goal is to have a "runtime
constant" associated with the function. In the your definition of
foo, expensive_call gets called every time that foo gets called;
this is what I'm trying to avoid!

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);
}

Here, expensive_call is called only once (assuming it never returns
undefined).

OK, I guess that in Python the only way to do what I want to do is
with objects...

kynn
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
--
http://mail.python.org/mailman/listinfo/python-list


cjw at ncf

Jul 29, 2008, 3:29 PM

Post #4 of 13 (347 views)
Permalink
Re: static variables in Python? [In reply to]

kj wrote:
> In <w8CdnQGPtuvdGRLVnZ2dnUVZ_r7inZ2d [at] comcast> Larry Bates <larry.bates [at] websafe`> writes:
>
>> 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?
>>>
>>> Thanks!
>>>
>>> kynn
>
>
>> First names in Python are just that, names that point to objects. Those objects
>> can contain any type of information including other objects. They are NOT
>> buckets where things are stored.
>
>> 1) Names (variables in Perl/C) defined within a Python function are placed in
>> its local namespace. They are not visible in the global namespace.
>
>> 2) Yes you can have a local name point to a global. This is often used in
>> classes with attributes because looking up local is somewhat quicker than
>> looking up the class attribute.
>
>> def foo():
>> x = expensive_call
>> return do_stuff_with(x())
>
> Maybe I'm missing your point, the goal is to have a "runtime
> constant" associated with the function. In the your definition of
> foo, expensive_call gets called every time that foo gets called;
> this is what I'm trying to avoid!
>
> 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);
> }
>
> Here, expensive_call is called only once (assuming it never returns
> undefined).
>
> OK, I guess that in Python the only way to do what I want to do is
> with objects...
>
> kynn

You might consider using a singleton class.

Colin W.
--
http://mail.python.org/mailman/listinfo/python-list


bearophileHUGS at lycos

Jul 29, 2008, 4:07 PM

Post #5 of 13 (344 views)
Permalink
Re: static variables in Python? [In reply to]

kj:
> OK, I guess that in Python the only way to do what I want to do
> is with objects...

There are other ways, like assigning the value out of the function,
because Python functions too are objects:

def iamslow():
return 100
def foo(x):
return x + foo.y
foo.y = iamslow() # slow computation
print foo(1)
print foo(2)

Output is:
101
102

Another way is this, a bit more clean, with the same output:

def iamslow():
return 100
def foo(x, y=iamslow()):
return x + y
print foo(1)
print foo(2)

But I suggest you to use a class in this situation, it's often the way
that will keep your code more bug-free, and more readable by near-
casual readers too. Python philosophy asks you to write readable code
instead of clever code when possible, this is a difference from Perl,
I presume.

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


bignose+hates-spam at benfinney

Jul 29, 2008, 4:27 PM

Post #6 of 13 (337 views)
Permalink
Re: static variables in Python? [In reply to]

kj <socyl [at] 987jk> writes:

> Is there a way to mimic C's static variables in Python? Or something
> like it?

A "static variable" in C is one that has access limited to the scope
in which it is declared.

Python approaches the same issue through namespaces: a name binding
made at a class or module level is accessible only via specification
of the class or module namespace.

> 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.

Python functions have local name bindings by default
<URL:http://www.python.org/doc/current/ref/naming.html>.

Python doesn't have "variables" in the sense of boxes containing
values, so it doesn't have "constants" in the sense of boxes that
don't change. Instead, Python has names bound to objects like sticky
notes. A name can later be re-bound to some other object.

What use case are you trying to address? It seems that the normal use
of local function names and class attributes would serve your
described requirements.

--
\ “It is hard to believe that a man is telling the truth when you |
`\ know that you would lie if you were in his place.” —Henry L. |
_o__) Mencken |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


is_this at visible

Jul 29, 2008, 6:23 PM

Post #7 of 13 (344 views)
Permalink
Re: static variables in Python? [In reply to]

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

> In <w8CdnQGPtuvdGRLVnZ2dnUVZ_r7inZ2d [at] comcast> Larry Bates <larry.bates [at] websafe`> 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()
--
http://mail.python.org/mailman/listinfo/python-list


Russ.Paielli at gmail

Jul 29, 2008, 6:33 PM

Post #8 of 13 (342 views)
Permalink
Re: static variables in Python? [In reply to]

On Jul 29, 1:40 pm, kj <so...@987jk.com.invalid> 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?
>
> Thanks!
>
> kynn
> --
> NOTE: In my address everything before the first period is backwards;
> and the last period, and everything after it, should be discarded.

If the constant parameters are really only needed in one particular
function, you can use default function arguments. An added benefit is
that you can override them with another value if necessary.

def fun(x, y, parameter1=0, parameter2=1):
...
--
http://mail.python.org/mailman/listinfo/python-list


ddasilva at umd

Jul 29, 2008, 9:30 PM

Post #9 of 13 (336 views)
Permalink
Re: static variables in Python? [In reply to]

This is the solution I suggest. It is fairly trivial, and works by
introducing the "self.static" namespace for a class's static
variables, in contrast to "self" for the class's instance variables.

-----------------------------------

class Static(object): pass
personStatic = Static()

class Person:
static = personStatic

def __init__(self, name, age):
self.name = name
self.age = age

def setVersion(self, version):
self.static.version = version

def getVersion(self):
return self.static.version
-----------------------------------

Daniel

On Tue, Jul 29, 2008 at 4:40 PM, kj <socyl [at] 987jk> 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?
>
> Thanks!
>
> kynn
> --
> NOTE: In my address everything before the first period is backwards;
> and the last period, and everything after it, should be discarded.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


alan.franzoni.blahblah at example

Jul 30, 2008, 1:15 AM

Post #10 of 13 (330 views)
Permalink
Re: static variables in Python? [In reply to]

kj was kind enough to say:

> 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?

There've been plenty of answers, and I'm not absolutely sure about what you
want... but closures are available in Python as well and you can use them,
and by combining them through the partial module you can get a sort of
closure factory:

from functools import partial

def getfunc(expensive_call, myfunc):
val = expensive_call()

f = partial(myfunc, val)

return f



you can then do something like that:

>> f = getfunc(lambda: 1, lambda x,y:x*y)
>> f(2)
6





--
Alan Franzoni <alan.franzoni.xyz [at] gmail>
-
Remove .xyz from my email in order to contact me.
-
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E
--
http://mail.python.org/mailman/listinfo/python-list


rhamph at gmail

Jul 30, 2008, 10:51 AM

Post #11 of 13 (320 views)
Permalink
Re: static variables in Python? [In reply to]

On Jul 29, 2:40pm, kj <so...@987jk.com.invalid> 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.

I'd go ahead and use globals. If these really are constant you should
just name them clearly (and possibly use all caps).

If they have a possibility of becoming non-constant in the future,
write a class. No fancy tricks needed to store state.
--
http://mail.python.org/mailman/listinfo/python-list


5lvqbwl02 at sneakemail

Jul 30, 2008, 1:05 PM

Post #12 of 13 (326 views)
Permalink
Re: static variables in Python? [In reply to]

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

--
http://mail.python.org/mailman/listinfo/python-list


schulz at sunbroy2

Oct 22, 2008, 9:05 AM

Post #13 of 13 (269 views)
Permalink
Re: static variables in Python? [In reply to]

In article <g6nv84$5pv$1 [at] reader1>, 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.

I know I'm coming late to the discussion, but the most natural way for
me would be to simulate the function via a callable object:


class hidden(object):
def __init__(self):
self.static_var = 0

def __call__(self):
self.static_var+=1
return self.static_var

fun_with_state = hidden()


>>> fun_with_state()
1
>>> fun_with_state()
2
>>> fun_with_state()
3
>>> fun_with_state()
4

Bye,

Stephan

--
-------------------------- It can be done! ---------------------------------
Please email me as schulz [at] eprover (Stephan Schulz)
----------------------------------------------------------------------------
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.