0

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

3
  • Is the indentation correct here? what is self? Also, generally loops like for i in range(len(...)) can be simplified. for sensor in self.sensors: # do something with a sensor Commented Dec 3, 2013 at 18:11
  • 1
    Well... that's the output I would expect... the return value of position is a reference to the position method... which you're then printing... When you ommited the () you were printing the reference directly, now that you're calling it, it's returning the self.position anyway... Commented Dec 3, 2013 at 18:13
  • Apologies, the indentation is correct. If it is incorrect then it is me pasting it wrongly in the question. Self explicitly shows that the variable is a member of the class. And I have also tried for sensor in self.sensors: #do something. I shall update my question to show this Commented Dec 3, 2013 at 18:14

2 Answers 2

2

You will need to invoke the methods:

for i in range(len(self.sensors)):
    print self.sensors[i].position()

Note the trailing parentheses.


I have changed a bit your example to make it runnable. This snippet works:

#! /usr/bin/python2.7

class Sensor:

    def __init__(self, loc, size=1, enabled=True):
        self.enabled = enabled
        self.distOfBorderFrmCentre = size / 2.0
        self.location = loc


    def position(self):
        return self.location

sensors = [ Sensor(loc) for loc in ('Loc 1', 'Loc 2', 'Loc 3') ]
for i in range(len(sensors)):
    print sensors[i].position()

#Previous two lines are a bit senseless, you can use:
for sensor in sensors:
    print sensor.position()
Sign up to request clarification or add additional context in comments.

8 Comments

Unfortunately, I still receive the same error after adding the parentheses. I have updated my original question nonetheless to add your change.
error? your current wording of the question contains no error
@ Hyperboreus - Seen and thank you. Combination of a long day and not spotting that I had incorrectly wrote "return self.position" instead of "return self.location"
@jpwagner - let's not be pernickety here. You know what I meant and so did others
@Chris Glad to help. Also note that everytime you write for i in range(len(someList)) a compiler commits suicide.
|
1

When you had:

for i in range(len(self.sensors)):
    print self.sensors[i].position

You were printing a reference to the the position method of the class, but now you've changed it to be:

for i in range(len(self.sensors)):
    print self.sensors[i].position()

You're now calling the function each time, which is great, except that the return value of the function:

def position(self):
    # No real use. Using this function for demo reasons
    return self.position

Is to return a reference to the function itself... which is effectively a round-about method of what you did the first time... Make your position function do something more meaningful... (maybe: return self.location or something for testing)

2 Comments

Wow... you've just pointed out the most incredible oversight on my part ever! Of course that wouldn't work... I just didn't spot it! Feel dumb now. Thanks
as @Jon pointed out, you're calling the function correctly now. i've deleted my answer pertaining to that issue, but note that you can also use the @property decorator to call the method as if it were a property of the class (ie, no "()" )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.