Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Python have static variables?

Tags:

python

There is a questions asking how to simulate static variables in python.

Also, on the web one can find many different solutions to create static variables. (Though I haven't seen one that I like yet.)

Why doesn't Python support static variables in methods? Is this considered unpythonic or has it something to do with Python's syntax?

Edit:

I asked specifically about the why of the design decision and I haven't provided any code example because I wanted to avoid explanation to simulate static variables.

like image 583
Georg Schölly Avatar asked Feb 26 '09 23:02

Georg Schölly


People also ask

Does Python have static variables?

Yes, definitely possible to write static variables and methods in python. Static Variables : Variable declared at class level are called static variable which can be accessed directly using class name.

Why static variables are evil?

Static variables are generally considered bad because they represent global state and are therefore much more difficult to reason about. In particular, they break the assumptions of object-oriented programming.

How do you declare a global static variable in Python?

On the other hand, the global variable in Python is a variable that can be used globally anywhere in the program. It can be used in any function or module, and even outside the functions, without having to re-declare it. Here in this example, we have re-declared the variable a in the function as a global variable.

What is a static class variable in Python?

Class or Static Variables in Python. The class may have a static variable whose value is “cse” for all objects. And class may also have non-static members like name and roll. In C++ and Java, we can use static keyword to make a variable as class variable. The variables which don’t have preceding static keyword are instance variables.

What is the difference between instance and class variables in Python?

The Python approach is simple, it doesn’t require a static keyword. All variables which are assigned a value in class declaration are class variables. And variables which are assigned values inside class methods are instance variables. # Python program to show that the variables with a value. # assigned in class declaration, are class variables.

What are non static variables in C++ and Java?

And class may also have non-static members like name and roll. In C++ and Java, we can use static keywords to make a variable a class variable. The variables which don’t have a preceding static keyword are instance variables.

How do you create a static method in Python?

Create staticmethod using the @staticmethod decorator and staticmethod () function A static method is a general utility method that performs a task in isolation. Static methods in Python are similar to those found in Java or C++. A static method is bound to the class and not the object of the class.


1 Answers

The idea behind this omission is that static variables are only useful in two situations: when you really should be using a class and when you really should be using a generator.

If you want to attach stateful information to a function, what you need is a class. A trivially simple class, perhaps, but a class nonetheless:

def foo(bar):     static my_bar # doesn't work      if not my_bar:         my_bar = bar      do_stuff(my_bar)  foo(bar) foo()  # -- becomes ->  class Foo(object):     def __init__(self, bar):         self.bar = bar      def __call__(self):         do_stuff(self.bar)  foo = Foo(bar) foo() foo() 

If you want your function's behavior to change each time it's called, what you need is a generator:

def foo(bar):     static my_bar # doesn't work      if not my_bar:         my_bar = bar      my_bar = my_bar * 3 % 5      return my_bar  foo(bar) foo()  # -- becomes ->  def foogen(bar):     my_bar = bar      while True:         my_bar = my_bar * 3 % 5         yield my_bar  foo = foogen(bar) foo.next() foo.next() 

Of course, static variables are useful for quick-and-dirty scripts where you don't want to deal with the hassle of big structures for little tasks. But there, you don't really need anything more than global — it may seem a but kludgy, but that's okay for small, one-off scripts:

def foo():     global bar     do_stuff(bar)  foo() foo() 
like image 51
Ben Blank Avatar answered Oct 09 '22 04:10

Ben Blank