I discovered some (for me) really strange behavior of python 3.6. I use pytest to test some behavior of a Vehicle
class, I defined. I create an instance of the class and initialize it with three vectors, namely position, velocity, and acceleration, slightly alter these vectors and then create a second instance of the same class and initialize it with slightly different vectors, explicitly defining one and relying on the default values provided in the __init__()
method. However, after the initialization, it seems that python does not use the default value of the constructor, but uses the last value of the first instance.
Here is the code I use to test my implementation:
def test_vehicle_movement():
vehicle = Vehicle2d(velocity=Vector(1.0, 0.0))
assert vehicle.velocity == Vector(1.0, 0.0)
assert vehicle.position == Vector(0.0, 0.0)
vehicle.move()
assert vehicle.position == Vector(1.0, 0.0)
vehicle2 = Vehicle2d(acceleration=Vector(2.0, 0.0))
assert vehicle2.position == Vector(0.0, 0.0) # fails
And here is the constructor of the vehicle
class:
class Vehicle2d:
def __init__(self, position: Vector = Vector(0.0, 0.0),
velocity: Vector = Vector(0.0, 0.0),
acceleration: Vector = Vector(0.0, 0.0)):
self.position = position
self.velocity = velocity
self.acceleration = acceleration
def move(self):
# changes the velocity and position vectors according to acceleration
I found this answer on another thread but in my case, different memory addresses are used, which is why I expect some error in the class vs. instance variable area. However, being rather new to python, I don't understand what I did wrong. To make things worse: The velocity vector gets reset as expected properly, only position is troublesome. Also, if I explicitly define position of vehicle2
to be a zero vector, this works fine. For some reason, I conclude, the default parameter is not used properly and or there is some unwanted connection between my vehicle
instances. Any idea anyone?