Search code examples
python-3.xdictionaryruntime-error

Python Dictionary Error Dict Attribute error


I have a little Problem in my Pythoncode. Im trying to give a dictionary from one object to a method of another object with this code: self.data_object.write_result_csv(self.return_data())

When I run the program this code works 7 from 8 times. In the last run I get this error message:

AttributeError: 'dict' object has no attribute 'write_result_csv'´

I had searched the entire day for the problem now and didn't find anything. Has anybody a hint for me.

Edit: Here is the code for the parts:

return_data method:

    def return_data(self):
    """Funktion um die Daten des Trades zurückzugeben"""
    return{'start_value': self.start_value, 'start_date': self.start_date, 'end_value': self.end_value, 'end_date': self.end_date, 'win': self.win}

write_result_csv method:

 def write_result_csv(self, results):
    with open(self.csv_file, 'a', newline='') as file:
        writer = csv.writer(file)
        writer.writerow([results['start_value'], results['start_date'], results['end_value'], results['end_date'], results['win']])

Thanks in advance.

Dixieslicer


Solution

  • Based on the error message,

    AttributeError: 'dict' object has no attribute 'write_result_csv'
    

    You are trying to call <dict>.write_result_csv(<args>). That means that in the above code, self.data_object is a simple dict() instead of the object you intend. Can you check that it is always initialized properly?