Using a class as a data container

前端 未结 12 1130
孤街浪徒
孤街浪徒 2020-12-02 10:01

Sometimes it makes sense to cluster related data together. I tend to do so with a dict, e.g.,

self.group = dict(a=1, b=2, c=3)
print self.group[\'a\']
         


        
12条回答
  •  余生分开走
    2020-12-02 10:24

    There is a new proposal that aims to implement exactly what you are looking for, called data classes. Take a look at it.

    Using a class over a dict is a matter of preference. Personally I prefer using a dict when the keys are not known a priori. (As a mapping container).

    Using a class to hold data means you can provide documentation to the class attributes.

    Personally, perhaps the biggest reason for me to use a class is to make use of the IDEs auto-complete feature! (technically a lame reason, but very useful in practise)

提交回复
热议问题