1

I was reading up about static variables (i.e. class variables), static methods, class methods and the nuances of all of these. In languages like C++, static variables are generally strongly discouraged because of many IMO compelling reasons. I was just curious, if the Python community has a known position on static variables being discouraged. The Google Python Style Guide does not seem to have a pro/con position on static variables.

In Python language, are static variables discouraged (i.e. have bad code smell)?

The Google C++ Style Guide's statement "[static variables] cause hard-to-find bugs" certainly seems to ring true to not just C++ language but also Python language.

Trevor Boyd Smith
  • 18,164
  • 32
  • 127
  • 177
  • That link talks about variables of class type with *static storage duration*, not static variables. That includes globals and `static`-marked locals. – user2357112 Mar 21 '17 at 16:57
  • This is probably too opinion-based. But I think generally, while there are certainly valid use-cases, you usually want an instance attribute. – juanpa.arrivillaga Mar 21 '17 at 16:57
  • Initialization order is much more precisely specified in Python than in C++. While it's still somewhat nondeterministic, you can usually ensure the C++ problems don't occur, as long as you don't have circular imports. – user2357112 Mar 21 '17 at 16:59

2 Answers2

2

In some languages, such as Java, static variables can cause problems with testability, as they cannot be easily mocked out. This is not so much the case in Python.

However, there is less benefit of static and class variables, because when you have values or functions that don't need access to the members of a particular instance, you can just define them at the module level, and use them without reference to a class -- a luxury not available in languages like Java.

So I would say that static in itself is not a code smell. But if half of your variables are static then I would ask some questions about your understanding of OO design.

Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
  • I am writing a big hierarchical tree. How to avoid root of the tree from being static varible? Pass the root as s parameter to every node constructor? Get a module level/global variable? – uuu777 Aug 28 '20 at 11:14
  • I don't understand your question -- I don't see any reason that you would need a variable for any superclass. Your question will get little visability here as a comment on my answer. Maybe turn your comment into a real question, and post it. – Eric Wilson Aug 31 '20 at 17:34
  • I posted the question.https://stackoverflow.com/questions/63690024/avoiding-static-members-and-methods-when-following-python-googl-style-guide – uuu777 Sep 01 '20 at 14:47
  • Sorry that it didn't get much attention. Happens sometimes. – Eric Wilson Sep 09 '20 at 00:40
1

The C++ resources you were reading are talking about variables of class type with static storage duration. Those are not the same thing as static variables; for example, the following variables also have static storage duration:

Example x;

namespace whatever {
    Example y;
}

void foo() {
    static Example z;
}

These are dangerous in C++ because the order in which they are initialized and destroyed is unpredictable and error-prone.


Python does not have variables of static storage duration. It doesn't even have storage durations at all. It has (module-)global and static variables, but their initialization order is much more precisely defined than in C++. The C++ problems are unlikely to occur unless you have a circular import, and even then, the circular import is the problem, not the global or static variables.


That said, mutable global state, such as globals or static variables that you mutate over the course of a program, is bad for language-independent reasons. This is a completely different concern from the issues C++ has with static storage duration. Globals and static variables you don't intend to mutate are usually fine.

Community
  • 1
  • 1
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • very insightful post. Two key takeaways from your post (aka summary): **1.** Python doesn't have the "static storage duration" and "initialization order is much more precisely defined than in C++" therefore an outright ban on static variables in Python is not needed. **2.** as you point out [generally speaking mutable global state is bad](http://softwareengineering.stackexchange.com/questions/148108) (your link is really *really* well written). I was kind of alluding to that side in my original post but the way you describe the problem is much better written. – Trevor Boyd Smith Mar 21 '17 at 19:51
  • This question almost feels like it should be posted on softwareengineering.stackexchange.com. but would have to be rewritten to be more generic. – Trevor Boyd Smith Mar 21 '17 at 19:53