Object 'instancemethod' does not have attribute '__getitem__' with class variables

I am trying to create a python class to control stepper motors using my raspberry Pi. This basically works, however, I keep getting the "instancemethod" object, it does not have the '__getitem__' attribute when I define the list as a class variable. The error message indicates this piece of code as the culprit, but I don’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. This is my code: 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 tell me what is wrong with him and explain why. thanks

+6
source share
1 answer

You did not publish full trace information, but I can assume:

 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 seq equal to stepper.clockwise method in the first line. Then a few lines later, you try to index it: seq[self.StepCounter] . But what does it mean to get the self.StepCounter method element?

Nothing because:

 'instancemethod' object has no attribute '__getitem__' 

You should not use clockwise either the list name or the method name; only the last completed definition will be executed, so by the time you get to seq = stepper.clockwise , this is a method, not a list.

+10
source

All Articles