Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

static variables...

0 views
Skip to first unread message

TracyBa

unread,
Jun 20, 1998, 3:00:00 AM6/20/98
to

In C you could declare a variable inside a function as static. This variable
would maintain its value from function call to function call. How do I declare
that type of variable in Python?

hin...@cnrs-orleans.fr

unread,
Jun 21, 1998, 3:00:00 AM6/21/98
to

tra...@aol.com (TracyBa) writes:

There is nothing exactly equivalent. In most cases, a good alternative
is to define a class representing callable objects (i.e. having a
__call__ method). The persistent data is then stored in ordinary
object attributes.
--
-------------------------------------------------------------------------------
Konrad Hinsen | E-Mail: hin...@cnrs-orleans.fr
Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.55.69
Rue Charles Sadron | Fax: +33-2.38.63.15.17
45071 Orleans Cedex 2 | Deutsch/Esperanto/English/
France | Nederlands/Francais
-------------------------------------------------------------------------------

Guido van Rossum

unread,
Jun 22, 1998, 3:00:00 AM6/22/98
to

> In C you could declare a variable inside a function as static. This
> variable would maintain its value from function call to function
> call. How do I declare that type of variable in Python?

Konrad already suggested using a class, which is generally good advice.

However, sometimes you need a quick fix to a program written in a
procedural style (and there's often nothing wrong with that). In that
case, consider using a module-global variable, e.g.:


ncalls = 0

def myfunction(arg):
global ncalls
ncalls = ncalls + 1
...your code goes here...

Because Python's globals are module-global, not program-global, this
is generally a safe practice.

--Guido van Rossum (home page: http://www.python.org/~guido/)

TracyBa

unread,
Jun 23, 1998, 3:00:00 AM6/23/98
to

>Konrad already suggested using a class, which is generally good advice.
>
>However, sometimes you need a quick fix to a program written in a
>procedural style (and there's often nothing wrong with that). In that
>case, consider using a module-global variable, e.g.:
>
>
> ncalls = 0
>
> def myfunction(arg):
> global ncalls
> ncalls = ncalls + 1
> ...your code goes here...
>
>Because Python's globals are module-global, not program-global, this
>is generally a safe practice.
>
>

Thanks for your suggestion. I played around with the classes and figured 'em
out. However, like you mentioned, there are a lot of times where a class would
be ineffecient to use for just one variable and the global typing could be
used.

I'm really happy with the Python language. The learning curve is a LOT faster
than C. Another BIG Python plus is this newsgroup. It amazes me how fast I
can get a response to my questions. Seldom have I been able to tell people
that the internet is constructive. This newsgroup has helped me out a lot.

Thanks again...


0 new messages