Python script for camera following a moving object during an animation
You might want to try some of the built-in tools instead of python (my compliments for your algo-oriented lifestyle!)
The solution is in Object Constraint properties.
For a created camera I created an empty that &quot;Copies transforms&quot; of the object I want to track (a &quot;car&quot; named &quot;Cube&quot;).   Then the camera Copies Transforms constraint of the empty &quot;before original&quot; to keep the original offset, and also &quot;Track To&quot; constraint of the Empty (to keep looking at it).
I then did a simple animation (keyed) of the Cube (standing in for your car).   You might have a roadway &quot;path&quot; (curve) that the car follows instead.  I include a link to my example blend file.