python-3.x - Python2.x/3.x 兼容性:“错误:AttributeError:‘Instruction’对象没有属性‘identity_byte’
问题描述
您好,我目前在运行程序时面临标题的错误消息。在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
实例中的成员变量。
谢谢你的时间。
解决方案
您忘记self
了第 11 行:
identity_byte = identification_byte
它应该是
self.identity_byte = identification_byte
顺便说一句,您需要在将来制作一个最小的可重现示例。这是我的:
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'
推荐阅读
- airflow - 如何使用 Airflow 从 s3 存储桶下载最新文件到本地机器
- ios - “ImagePicker”类型的值没有成员“contentMode”
- reactjs - 为什么我的道具在发送到组件时未定义?
- c++ - 如何防止调用未初始化的 C++ 对象的非静态函数
- java - Java 枚举构造函数
- visual-studio - 无法升级超过 16.1.3 的 Visual Studio 社区版
- http - OkHttp3:在读取大型 HTTP 响应时出现“意外的流结束”异常
- android - 为什么我无法使用 Kotlin 更改 ImageView 的可见性?
- arduino - Rc522 无法更改 rfid 标签 Aduino ide 1.8.12 的 UID
- python - 在 python 中的类中使用装饰器处理异常