1

I have a simple function that works when I enter it in my Jython interpreter manually, but the call to len() fails when I try to run the code as a function.

 def calculateChecksum(self, command):
      sum = 0
      for b in range(len(command)):
        sum = sum + command[b-1]
      mod =  sum % 64
      checkbyte = mod & (0xFF)
      checksum = checkbyte | 0x80

where command is a jarray.array of bytes (Why am I not using the built-in array type? I ask you: Does it matter? jarray.array is working for everything else, and it clearly works on some occasions, see below)

>>> testarray
array([55, 57, 51], byte)
>>> len(testarray)
3
>>> stage.calculateChecksum(stage, testarray)
Traceback (innermost last):

  File "<console>", line 1, in ?

  File "....py", line 75, in calculateChecksum

AttributeError: __len__

So I think it's safe to say that this array implements len(), but I don't know why that doesn't always seem to be true. Any idea what's going on here?

emw
  • 13
  • 2

2 Answers2

2

Call the method like this:

stage.calculateChecksum(testarray)

Notice that you don't have to explicitly pass stage for the self parameter, that's passed implicitly when you invoke a method over an object (stage in this case.)

Óscar López
  • 232,561
  • 37
  • 312
  • 386
0

You defined def calculateChecksum(self, command): into a class, and when you call a class-method, you do not need to add the self variable. Python adds it for you.

alinsoar
  • 15,386
  • 4
  • 57
  • 74