473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

static variables in Python?

kj


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.
Jul 29 '08 #1
15 2937
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
Jul 29 '08 #2
kj
In <w8******************************@comcast.comLar ry Bates <la*********@websafe.com`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.
Jul 29 '08 #3
kj wrote:
In <w8******************************@comcast.comLar ry Bates <la*********@websafe.com`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.
Jul 29 '08 #4
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
Jul 29 '08 #5
kj <so***@987jk.com.invalidwrites:
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
Jul 29 '08 #6
On Tue, 29 Jul 2008 21:31:01 +0000, kj wrote:
In <w8******************************@comcast.comLar ry Bates <la*********@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()
Jul 30 '08 #7
On Jul 29, 1:40 pm, kj <so...@987jk.com.invalidwrote:
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):
...
Jul 30 '08 #8
be************@lycos.com wrote:
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:
....
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
Here's a solution using decorators, I like it, but I'm biased:

def staticAttrs(**kwds):
"""
Adds attributes to a function, akin to c-style
"static" variables
"""

def _decorator(fcn):
for k in kwds:
setattr(fcn, k, kwds[k])
return fcn
return _decorator

@staticAttrs(n=0)
def rememberCalls():
"""
>>rememberCalls()
0
>>rememberCalls()
1
>>rememberCalls()
2
"""
print rememberCalls.n
rememberCalls.n += 1

~Scott
Jul 30 '08 #9
On Jul 29, 6:33 pm, "Russ P." <Russ.Paie...@gmail.comwrote:
On Jul 29, 1:40 pm, kj <so...@987jk.com.invalidwrote:
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):
...
I should add that the parameters need not be literal numbers. They can
be computed values as well. They will be computed only once, on the
first pass through the function definition, which I presume is exactly
what you want.

I think this is the simplest solution to the problem you posed.
Jul 30 '08 #10
On Jul 30, 11:57 am, "Russ P." <Russ.Paie...@gmail.comwrote:
On Jul 29, 6:33 pm, "Russ P." <Russ.Paie...@gmail.comwrote:
On Jul 29, 1:40 pm, kj <so...@987jk.com.invalidwrote:
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):
...

I should add that the parameters need not be literal numbers. They can
be computed values as well. They will be computed only once, on the
first pass through the function definition, which I presume is exactly
what you want.

I think this is the simplest solution to the problem you posed.
Here's a real-life example, where the second and third args are run-
time constants:

def unescape(s,
subber=re.compile(r'_x[0-9A-Fa-f]{4,4}_').sub,
repl=lambda mobj: unichr(int(mobj.group(0)[2:6], 16)),
):
if "_" in s:
return subber(repl, s)
return s
# The if test is just an optimisation that unfortunately the re module
doesn't nut out for itself.

Cheers,
John
Jul 30 '08 #11
On Jul 29, 8:38*pm, pigmartian <scottp...@comcast.netwrote:
bearophileH...@lycos.com wrote:
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:

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

Here's a solution using decorators, I like it, but I'm biased:

def staticAttrs(**kwds):
* * * * """
* * * * Adds attributes to a function, akin to c-style
* * * * "static" variables
* * * * """

* * * * def _decorator(fcn):
* * * * * * * * for k in kwds:
* * * * * * * * * * * * setattr(fcn, k, kwds[k])
* * * * * * * * return fcn
* * * * return _decorator

@staticAttrs(n=0)
def rememberCalls():
* * * * """
* * * * >>rememberCalls()
* * * * 0
* * * * >>rememberCalls()
* * * * 1
* * * * >>rememberCalls()
* * * * 2
* * * * """
* * * * print rememberCalls.n
* * * * rememberCalls.n += 1

~Scott
I like it too. It also thought of (implementation not shown):

@has_locals
def rememberCalls( self ):
self.val= 0
self.ref= object( )

where self is preserved between calls and is an instance of a custom
class, possibly empty. If you want more than one, but still
preserved:

rememberCallsA= has_locals( rememberCalls )
rememberCallsB= has_locals( rememberCalls )

You might want to make self a small and lightweight dict-only object:

@has_locals
def rememberCalls( dic ):
dic['val']= 0
dic['ref']= object( )
Jul 30 '08 #12
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 <so***@987jk.com.invalidwrote:
>

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
Jul 30 '08 #13
On Jul 29, 2:40*pm, kj <so...@987jk.com.invalidwrote:
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.
Jul 30 '08 #14

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

Jul 30 '08 #15
In article <g6**********@reader1.panix.com>, 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 sc****@eprover.org (Stephan Schulz)
----------------------------------------------------------------------------
Oct 22 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daniel Schle | last post by:
Hi all I have 2 questions 1) is there a way do declare static variables in a class? I coded this logic in global variable foo_cnt 2) is there a way to see the types of all data/function...
2
by: | last post by:
I've been looking for a way of have static variables in a python class and all I have found is this: class A: static = def f(self): A.static =
8
by: Grzegorz Dostatni | last post by:
I had an idea yesterday. (Yes, I know. Sorry). If we don't need to declare variables as having a certain type, why do we need to import modules into the program? Isn't the "import sys" redundant...
3
by: mudd | last post by:
How do I build Python so that I get static libraries instead of dynamic libraries (e.g. build/lib.solaris-2.8-sun4u-2.3/math.so)? John
4
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned...
9
by: Florian Lindner | last post by:
Hello, does python have static variables? I mean function-local variables that keep their state between invocations of the function. Thanks, Florian
3
by: dmitrey | last post by:
Thank you in advance, Dmitrey
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
0
by: Luis Zarrabeitia | last post by:
Quoting Joe Strout <joe@strout.net>: I'm sure your credentials are bigger than mine. But he is right. A lot of languages have ditched the "concept" of a static variable on a method (how do you...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.