简体   繁体   中英

Python: AttributeError: 'int' object has no attribute error when I passing user defined object

I have a Microphone class with this structure:

class Microphone(object):  # Microphone class

def __init__(self, x, y, limit):
    self.X = x
    self.Y = y
    self.low_limit = limit
    self.counter = 0

And a function which working with a list of Microphone instances.

def knock_calibration_2d(port, microphones, W):

print " ------------------------- Knock calibration ----------------------"
i = 0
while True:
    if port.in_waiting > 0:
        msg = str(port.readline()).strip()
        if msg.startswith("ACK:"):  # received an ACK message
            continue
        elif msg.startswith("A"):  # received an ADC value
            words = msg.split(' ')
            microphones[(ord(msg[1]) - ord('0'))] = int(words[1])
            i = i + 1
        if i == len(microphones):
            break

print "Raw calibration counter values: " + str([e for e in microphones])

L = W / math.sqrt(2)  # W=47
T = 0.0

for j in range(1, len(microphones)):
    T = T + (microphones[j].counter - microphones[0].counter)
T = T / (len(microphones) - 1)
C = L / T
print "Normalized calibration counter values: " + str([e for e in microphones])
print "L=" + str(L) + " T=" + str(T) + " C=" + str(C)

return 1

With the following expression I always get an "AttributeError: 'int' object has no attribute 'counter'" error message, meanwhile, I'm pretty sure that the microphones list contains just Microphone objects instead of int, which has counter attribute. What will be the problem here? I created a pastebin with the full code here . I'm working with pycharm with anaconda and with python 2.7 interpreter

T = T + (microphones[j].counter - microphones[0].counter)

Here is my main function where I defined the list:

def main():

microphones = [
                Microphone(  0,    0,   50),
                Microphone(  W/2,  W/2, 50),
                Microphone(  W/2, -W/2, 50),
                Microphone( -W/2, -W/2, 50),
                Microphone( -W/2,  W/2, 50)
               ]

C = knock_calibration_2d(port, microphones, W)

In your knock_calibration_2d function ,line 10~12,you change the microphones' element.

You substituted the element with an Int object if message starts with an “A”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM