Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing methods from objects within a list
    primarykey
    data
    text
    <p>I am new to Python (having come from C++) so I am still getting used to some things.</p> <p>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. </p> <p>I have the following code:</p> <pre><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() </code></pre> <p>The output I am given is:</p> <pre><code>&lt;bound method Sensor.position of &lt;sensor.Sensor instance at 0x03628E40&gt;&gt; &lt;bound method Sensor.position of &lt;sensor.Sensor instance at 0x03628DC8&gt;&gt; &lt;bound method Sensor.position of &lt;sensor.Sensor instance at 0x03628DF0&gt;&gt; &lt;bound method Sensor.position of &lt;sensor.Sensor instance at 0x03628E18&gt;&gt; &lt;bound method Sensor.position of &lt;sensor.Sensor instance at 0x03628E40&gt;&gt; </code></pre> <p>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.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload