调用方法<名称>我得到AttributeError:'int'对象没有属性<名称>

时间:2019-07-04 06:01:54

标签: python-3.x class

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

  
    
      

回溯(最近通话最近):         在NewPins中,文件“ /home/coder/Try2.py”,第35行           x = self.mcp.readGPIO()       AttributeError:“ int”对象没有属性“ mcp”

    
  

如果我输入-     a.mcp.readGPIO() <-我从python shell获得

`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”

1 个答案:

答案 0 :(得分:0)

好吧,我的笨蛋...-我正在使用的RPiGPIO库返回回调中的GPIO通道号,因此回调函数需要两个参数而不是一个。我曾以为自己会弄乱我的命名空间,所以当我遇到上述错误时,我正试图对此进行编码-我是python新手,也不知道在哪里看! 那会教我不要彻底阅读文档...