Python Class vs. Module Attributes

前端 未结 4 737
抹茶落季
抹茶落季 2020-12-29 09:47

I\'m interested in hearing some discussion about class attributes in Python. For example, what is a good use case for class attributes? For the most part, I can not come up

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-29 10:37

    Encapsulation is a good principle: when an attribute is inside the class it pertains to instead of being in the global scope, this gives additional information to people reading the code.

    In your situations 1-4, I would thus avoid globals as much as I can, and prefer using class attributes, which allow one to benefit from encapsulation.

提交回复
热议问题