0

I have created a custom class GPSPosition. This class has the standard method str() defined, which would return a long string with all the values of the object returned in a specific format. If I write the string into a file, my GPS Device would be able to read the position information from the file.

I have a list of GPSPosition object, with all the values populated by the user. The next task is to loop through the list, and get the string representation of each GPSPosition object and write it to the file.

writeFileHandler.write(GPSPositionObject)

This does not work, as I get an error saying that write expects a string, but I am passing an object.

user3831696
  • 87
  • 1
  • 6

1 Answers1

0

It's because in that manner, you are passing in the object. You have to call the str() function on it to use your defined str function:

 writeFileHandler.write(str(GPSPositionObject))

EDIT: As a side note, you may also want to look into defining the repr function, as this uses str: Difference between str and repr in python

Community
  • 1
  • 1
the_constant
  • 681
  • 4
  • 11