Javalin597 Javalin597 - 1 year ago 37
Python Question

str TypeError, yes it's basic but hear me out

I know, this is probably a very simple problem, but no matter how many times I go through this, this isn't making any sense. I made a very simple class that is meant for controlling an Arduino-based car. I set it up so that the serialOut function will return a string with the necessary formatting to send over serial. However, whenever I use it, I keep getting this error:

Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
TypeError: 'str' object is not callable

this is the code for the class:

# Drive Communication System
import serial

def output(speed,steer,orientation):
return ('%02d' % speed) + ('%02d' % steer) + (orientation)

class DriveTrain:

def __init__(self,serial,cruise_speed = 60, turn_sensitivity = 1):
self.serialOut = '0045s'
self._cruise = cruise_speed
self._trim = turn_sensitivity
self._speed = 0
self._steer = 0
self._orientation = 's'

def Drive(self,speed, steer = 0, orientation = 's'):
self._steer = steer + 45
self._speed = speed
self._orientation = orientation

def stop(self):
self._steer = 45
self._speed = 0
self._orientation = 's'

def status(self):
return (self._speed,self._steer - 45,self._orientation)

def serialOut(self):
return output(self._speed,self._steer,self._orientation)

if __name__ == '__main__':
ser = serial.Serial('COM3',38400)
car = DriveTrain(ser)

Originally, I had the operations currently in the output function in the actual serialOut class function, but the same things happened. I also attached a picture that shows how I went through checking each operation, and yet for some reason it still won't work. I'm obviously missing something simple, but for the life of me, I just can't figure out what it is...

Testing the code

Answer Source

In __init__, you have:

self.serialOut = '0045s'

This has the effect of overriding the serialOut method (since it has been assigned to each class instance). Just remove the assignment (or use a different name).