【发布时间】:2019-10-09 18:51:31
【问题描述】:
您好,我目前在运行程序时遇到标题的错误消息。 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 2.6 和 3.2 都很老旧,不再受支持。
标签: python-3.x python-2.6