Instance variables vs. class variables in Python

后端 未结 4 902
我在风中等你
我在风中等你 2020-11-22 05:32

I have Python classes, of which I need only one instance at runtime, so it would be sufficient to have the attributes only once per class and not per instance. If there woul

4条回答
  •  醉梦人生
    2020-11-22 06:25

    If you have only one instance anyway, it's best to make all variables per-instance, simply because they will be accessed (a little bit) faster (one less level of "lookup" due to the "inheritance" from class to instance), and there are no downsides to weigh against this small advantage.

提交回复
热议问题