I am new to Python (having come from C++) so I am still getting used to some things.
I have created a class called Sensor which I am using as a sensor that detects when an avatar or a player moves in to it. I want to have up to 20 sensors as one time so I would like to store them in a list and then iterate through the list, checking each one for a detected entry.
I have the following code:
class Sensor:
def __init__(self, loc, size=1, enabled=True):
self.enabled = enabled
self.distOfBorderFrmCentre = size / 2.0
self.location = loc
# Additional Sensor code here. Just not implemented yet so would not
# benefit the post.
def position(self):
# No real use. Using this function for demo reasons
return self.position
self.sensors = [ Sensor(loc) for loc in self.route ]
for i in range(len(self.sensors)):
print self.sensors[i].position()
# Also gives same output
#for sensor in self.sensors:
#print sensor .position()
The output I am given is:
<bound method Sensor.position of <sensor.Sensor instance at 0x03628E40>>
<bound method Sensor.position of <sensor.Sensor instance at 0x03628DC8>>
<bound method Sensor.position of <sensor.Sensor instance at 0x03628DF0>>
<bound method Sensor.position of <sensor.Sensor instance at 0x03628E18>>
<bound method Sensor.position of <sensor.Sensor instance at 0x03628E40>>
So am I missing something? I suspect I probably am. I have searched and searched but every time I see examples of calling methods from objects within lists, the syntax I listed above is what is used.
Thanks
self? Also, generally loops likefor i in range(len(...))can be simplified.for sensor in self.sensors: # do something with a sensorpositionis a reference to thepositionmethod... which you're then printing... When you ommited the()you were printing the reference directly, now that you're calling it, it's returning theself.positionanyway...