Home > Software engineering >  New instance of a class contains older data given to an older instance
New instance of a class contains older data given to an older instance

Time:10-08

I'm trying to analyze some reviews using vader in python. I made an Analyzer class like so:

class Analyzer:
    dataframe = None
    reviews = []
    
    # other data

    def __init__(self, brand):
        self.dataframe = pd.read_csv(brand) # using pandas
        reviews = self.dataframe['Reviews'].tolist()

    def analyze(self):
        # code for analyze...

    # more codes...

and I used this class in main.py like so:

from analyzer import Analyzer

brands = [
    'apple',
    'google',
    'huawei',
    'motorola',
    'nokia',
    'samsung',
    'sony',
    'xiaomi'
]

for brand in brands:
    analysis = Analyzer(brand)
    analysis.analyze()
    del analysis

now the problem is: When brands are given to the class to be analyzed, the older reviews remain in the list.

e.g.: apple.csv has 1000 reviews and google.csv has 700 reviews. but when google is passed to analyzer, the reviews list length is not 700, but 1700.

CodePudding user response:

reviews attribute is for class and not object, since you defined it in the body of the class.
define the reviews in the body of __init__:

class Analyzer:
    
    # other data

    def __init__(self, brand):
        self.dataframe = pd.read_csv(brand) # using pandas
        self.reviews = self.dataframe['Reviews'].tolist()

    def analyze(self):
        # code for analyze...

    # more codes...
  • Related