Why doesn't Python have static variables?

后端 未结 9 2501
暗喜
暗喜 2020-11-27 14:48

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

9条回答
  •  孤街浪徒
    2020-11-27 15:36

    From one of your comments: "I'd like to use them to cache things loaded from disk. I think it clutters the instance less, if I could assign them to the function"

    Use a caching class then, as a class or instance attribute to your other class. That way, you can use the full feature set of classes without cluttering other things. Also, you get a reusable tool.

    This shows that on SO it always pays off to state one's problem instead of asking for a specific, low level solution (e.g. for a missing language feature). That way, instead of endless debates about simulating "static" (a deprecated feature from an ancient language, in my view) someone could have given a good answer to you problem sooner.

提交回复
热议问题