I've just written this class:
class PhysicsObject: "An object that is physically simulated. Has velocity, position, and orientation." def __init__(self): self.velocity=Vector(0,0) self.position=Vector(0,0) self.heading=0 #This gives a direction vector that represents the direction the physics object is facing self.forward=property(fget=lambda self: Vector(1,0).rotate(self.heading)) #This gives an integer that represents how fast the object is moving in the direction it's facing self.fwdspeed=property(fget=lambda self: self.velocity.dot(self.forward)) self.mass=1 To test it, I wrote this little bit of code:
myphysobj=PhysicsObject() myphysobj.velocity=Vector(15,5) print("Position:",myphysobj.position,"Facing:",myphysobj.forward,"Forward Speed:",myphysobj.fwdspeed) I expected the result to be something along these lines:
Position: (0,0) Facing: (0,0) Forward Speed: 5 However, I instead got
Position: (0,0) Facing: <property object at 0x02AB2150> Forward Speed: <property object at 0x02AB2180> As I understand it, setting an attribute to the result of property(fget=myfunc) should give the result of myfunc() when that attribute is accessed. Instead, it seems to be giving me the property object itself. Am I misunderstanding how property() is used, or have I committed a more subtle error?