【问题标题】:Calling a method <name> I get AttributeError: 'int' object has no attribute <name>调用方法 <name> 我得到 AttributeError: 'int' object has no attribute <name>
【发布时间】:2019-07-04 06:01:54
【问题描述】:

我在 GPIO 引脚上触发了一个回调方法,该方法在触发时会调用读取输入设备寄存器,但是在调用时我得到 -

Traceback(最近一次调用最后一次): 文件“/home/coder/Try2.py”,第 35 行,在 NewPins 中 x = self.mcp.readGPIO() AttributeError:“int”对象没有属性“mcp”

如果我输入- a.mcp.readGPIO()

`BitArray('0xffff')`  <-expected result
>>> 

RotaryEncoder 类被实例化为 'a'

 `class RotaryEncoder:`
   `def __init__(self,IntPin, Start, Id):`
     .... initialise some variables then open the device as 'mcp' -
   `self.mcp = MCP23S17(bus=0x00, pin_cs=0x00, device_id=Id)`
   `self.mcp.open()`
   `def NewPins(self):`
   `   global OldPins`
   `   x = self.mcp.readGPIO()`
        ..... irrelevant code

回调线是 gpio.add_event_detect(IntPin, gpio.FALLING, callback=NewPins)

预期的读取值为 BitArray 实际结果是 AttributeError:“int”对象没有属性“mcp”

【问题讨论】:

    标签: python-3.x class


    【解决方案1】:

    好吧,我的笨蛋.... - 我正在使用的 RPiGPIO 库在回调中返回 GPIO 通道号,因此回调函数需要两个参数而不是一个。我以为我弄乱了我的命名空间,所以当我遇到上面的错误时试图围绕这个进行编码——我是一个新手 python 程序员,不知道去哪里找! 这将教会我不要彻底阅读文档......

    【讨论】:

      猜你喜欢
      • 2014-12-20
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      • 2022-12-01
      • 2017-10-26
      相关资源
      最近更新 更多