dictionary shared between objects for no reason?

后端 未结 4 1398
情书的邮戳
情书的邮戳 2021-01-22 18:59

The following code is supposed to create a new (modified) version of a frequency distribution (nltk.FreqDist). Both variables should then be the same length.

It works f

4条回答
  •  Happy的楠姐
    2021-01-22 19:39

    class WebText:
        freq_dist_weighted = {}
    

    declares freq_dist_weighted so that it is shared between all objects of type WebText; essentially, this is like a static member in C++.

    If you want each WebText object to have its own freq_dist_weighted member (i.e. you can change it for one instance without changing it for another instance), you want to define it in __init__:

    class WebText:
        def __init__(self):
            self.freq_dist_weighted = {}
    

提交回复
热议问题