Python2.x / 3.x兼容性:“错误:AttributeError:'Instruction'对象没有属性'identity_byte'

问题描述 投票:0回答:1

您好,我目前在运行程序时遇到标题错误消息。 identity_byte是在构造函数上明确定义的,所以我不知道他为什么找不到它。

实际上,Python 2.6确实找到了它,并且运行我的程序没有问题。但是python 3.2会显示错误消息。

在Python 3.2下运行它的全部输出和一个几乎独立的代码示例(该代码是自包含的,但是需要一个文件作为输入)

class 'bytes'
class 'int'
120
216
169
Traceback (most recent call last):
  File "test.py", line 76, in <module>
    main () 
  File "test.py", line 73, in main
    cpu.process_instructions ()
  File "test.py", line 58, in process_instructions
    instruction.process ()
  File "test.py", line 18, in process
    print ("Identifier Byte: {}".format(self.identity_byte))
AttributeError: 'Instruction' object has no attribute 'identity_byte'
from abc import ABCMeta, abstractmethod
import argparse

HEADER_SIZE = 16
KB_SIZE = 16384

class Instruction (object):
    __metaclass_ = ABCMeta

    def __init__ (self,identification_byte):
        identity_byte = identification_byte

    def test (self):
        print ("BOTO")

    @abstractmethod
    def process (self):
        print ("Identifier Byte: {}".format(self.identity_byte))

    @abstractmethod
        def process2 (self):
            print ("Identifier Byte2: ", self.identity_byte)


class LDAInstruction (Instruction):
    def process (self):
       super.process ()

    def process2 (self):
       super.process()

 class ROM (object) :
    def __init__ (self, rom_bytes): 
       self.header = rom_bytes [0:HEADER_SIZE]
       self.temp = self.header [4]
       print (type (self.header))
       self.num_prg_blocks = 2#struct.unpack ("I", self.temp)
       print (type (self.num_prg_blocks))
       self.data_bytes = rom_bytes [HEADER_SIZE:HEADER_SIZE + (16 + KB_SIZE * int (self.num_prg_blocks))]
       self.total_size = 16 + KB_SIZE * self.num_prg_blocks

    def get_byte (self, pc):
        return  (self.data_bytes [pc])

class CPU (object):
    def __init__(self, rom_bytes):
        self.registers = []
        self.rom = ROM (rom_bytes)
        self.pc = 0

    def process_instructions (self):
       for byte in self.rom.data_bytes:
          byte = self.rom.get_byte (self.pc)
          self.pc+=1
          print (byte)
          if (byte == 169):
             instruction = Instruction (byte)
             instruction.process ()
             instruction.process2 ()
          if (self.pc == 3):
             break

def main ():

   parser = argparse.ArgumentParser (description='NES EMULATOR'); 
   parser.add_argument ('rom_path',metavar='R',type=str,help='path to the rom')
   args=parser.parse_args()

   with open (args.rom_path, 'rb') as file:
     rom_bytes = file.read ()           

   cpu = CPU(rom_bytes)
   cpu.process_instructions ()

if __name__ == '__main__':
    main () 

我不知道为什么会这样,因为我使用以下命令创建了Instruction的实例:

instruction = Instruction (byte)

所以我希望它已经是实例中它的成员变量identity_byte

感谢您的时间。

python-3.x python-2.6
1个回答
0
投票

您在第11行上忘记了self

identity_byte = identification_byte

应该是

self.identity_byte = identification_byte

顺便说一下,您将来需要制作minimal reproducible example。这是我的:

class Foo:
    def __init__(self, bar):
        bar = bar

f = Foo(9)
print(f.bar)

Python 3.6中的输出:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    print(f.bar)
AttributeError: 'Foo' object has no attribute 'bar'

Python 2.6中的输出与最后一行相同:

AttributeError: Foo instance has no attribute 'bar'
© www.soinside.com 2019 - 2024. All rights reserved.