首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python: AttributeError:'int‘对象在传递用户定义对象时没有属性错误

Python: AttributeError:'int‘对象在传递用户定义对象时没有属性错误
EN

Stack Overflow用户
提问于 2018-06-23 12:07:54
回答 1查看 1.3K关注 0票数 0

我有一个具有这种结构的麦克风类:

代码语言:javascript
复制
class Microphone(object):  # Microphone class

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

以及与麦克风实例列表一起工作的函数。

代码语言:javascript
复制
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

使用下面的表达式,我总是会得到"AttributeError:' int‘对象没有属性’计数器‘“错误消息,同时,我确信麦克风列表只包含麦克风对象,而不是带有计数器属性的int。这里会有什么问题?我用完整的代码这里创建了一个pastebin。我和anaconda和python2.7的解释器一起工作

T=T+ (microphonesj.counter - microphones.counter)

下面是我定义列表的主要功能:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-23 12:26:30

在你的knock_calibration_2d函数中,第10~12行,你改变麦克风的元素。

如果消息以“A”开头,则使用Int对象替换元素

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51001009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档