Python “instancemethod”对象没有带有类变量的属性“__getitem__”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21365521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:41:41  来源:igfitidea点击:

'instancemethod' object has no attribute '__getitem__' with class variables

pythonraspberry-pi

提问by SquarePie

I'm trying to create a python class to control stepper motors using my Raspberry Pi. It mostly works, however I keep on getting an "'instancemethod' object has no attribute '__getitem__'error whenever I define a list as a class variable. The error message lists this piece of code as the culprit but I can't see anything wrong with it if seq[self.StepCounter][pin]!=0:. It will work if I define it as an instance variable or a global variable though. This is my code: import RPi.GPIO as GPIO import time debug = True

我正在尝试创建一个 python 类来使用我的 Raspberry Pi 控制步进电机。它主要有效,但是'__getitem__'每当我将列表定义为类变量时,我都会不断收到“'instancemethod'对象没有属性错误。错误消息将这段代码列为罪魁祸首,但我看不出有什么问题if seq[self.StepCounter][pin]!=0:。如果我将其定义为实例变量或全局变量,它将起作用。这是我的代码:import RPi.GPIO as GPIO import time debug = True

class stepper:
    clockwise = []
    clockwise = range(0,4)
    clockwise[0] = [1,0,0,0]
    clockwise[1] = [0,1,0,0]
    clockwise[2] = [0,0,1,0]
    clockwise[3] = [0,0,0,1]
    def __init__(self,pin1,pin2,pin3,pin4):
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BCM)
        self.pin1 = pin1
        self.pin2 = pin2
        self.pin3 = pin3
        self.pin4 = pin4
        self.StepCounter = 0
        self.pinarray = [pin1,pin2,pin3,pin4]
        for pin in self.pinarray:
            if debug == True:
                print "Setup pin " + str(pin)
            GPIO.setup(pin,GPIO.OUT)
            GPIO.output(pin, False)
        self.stepNum = 512.0
        self.coilNum = 4.0

    def setup(self,stepNum,coilNum):
        self.stepNum = float(stepNum)
        self.coilNum = float(coilNum)
        self.partNum = self.coilNum * self.stepNum

    def clockwise(self,speed):
        seq = stepper.clockwise
        self.WaitTime = (1.0 / (self.stepNum * self.coilNum)) * speed
        for pin in range(0, 4):
            xpin = self.pinarray[pin]
            if seq[self.StepCounter][pin]!=0:
              GPIO.output(xpin, True)
            else:
              GPIO.output(xpin, False)
        self.StepCounter += 1
        if (self.StepCounter==len(seq)):
            self.StepCounter = 0
        if (self.StepCounter<0):
            self.StepCounter = len(seq)
        time.sleep(self.WaitTime)

print "Adding Motor Instance"    
motor = stepper(24,25,8,7)
print "Spinning Motor"
while "True":
    motor.clockwise(5)

Please could someone tell me what's wrong with it and explain why. Thanks

请有人告诉我它有什么问题并解释原因。谢谢

采纳答案by DSM

You didn't post the full traceback, but I can take a guess:

您没有发布完整的回溯,但我可以猜测:

def clockwise(self,speed):
    seq = stepper.clockwise
    self.WaitTime = (1.0 / (self.stepNum * self.coilNum)) * speed
    for pin in range(0, 4):
        xpin = self.pinarray[pin]
        if seq[self.StepCounter][pin]!=0:

You set seqequal to the method stepper.clockwiseon the first line. Then a few lines later you try to index into it: seq[self.StepCounter]. But what does it mean to get the self.StepCounter-th element of a method?

您设置为seq等于stepper.clockwise第一行的方法。然后几行之后,您尝试对其进行索引:seq[self.StepCounter]. 但是获取方法的self.StepCounter第 -th 个元素是什么意思?

Nothing, because:

没什么,因为:

'instancemethod' object has no attribute '__getitem__'

You shouldn't use clockwiseboth as the name of a list and as the name of a method; only the last-executed definition will hold, so by the time you get to seq = stepper.clockwise, it's the method, not the list.

你不应该同时使用clockwise列表的名称和方法的名称;只有最后执行的定义会成立,所以当你到达时seq = stepper.clockwise,它是方法,而不是列表。