0

eepromというリストの特定の内容を受け取り、 bytesというリストの下に保存しようとしています。

class Parameter (object):
    def __init__ (self, eeprom, *address):
        self.eeprom = eeprom
        self.bytes = list()
        for a in address:
            a = int(a, 16)
            byte = eeprom[a]                 # here lies the problem
            self.bytes.append(byte)

sthg = Parameter(eeprom, "0x00B9", "0x00BA")

スクリプトを実行するたびに、次のエラーが発生します。

TypeError: 'int' object has no attribute '__getitem__'

なぜこれが起こるのか誰にも分かりますか?これをインタープリターに書いた場合は機能しますが、このエラーが発生するのはモジュールとして実行した場合のみです。

4

3 に答える 3

3

When you are instantiating Parameter you are most likely passing an int in for the eeprom arguments instead of a list. You are probably doing the equivalent of

sthg = Parameter(1, "0x00B9", "0x00BA")

when you should be doing

sthg = Parameter([1,2,3,4], "0x00B9", "0x00BA")
于 2013-07-03T12:11:58.383 に答える
0
class Parameter (object):
    def __init__ (self, eeprom, *address):
        self.eeprom = eeprom
        self.bytes = list()
        for a in address:
            a = int(a,16)
            byte = eeprom[a]                 # here lies the problem
            self.bytes.append(byte)


eeprom = [1,2,3,4,5,6,7,8,9,10,11]
sthg = Parameter(eeprom, "0x0009")

eeprom は整数ではなくリスト オブジェクトである必要があります

例:

eeprom = [1,2,3,4,5,6,7,8,9,10,11]
于 2013-07-03T13:24:51.800 に答える