Python Forum

Full Version: Object cannot find its attribute
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I have this line of code

print ("Identifier Byte: {}".format(self.identity_byte))
that generates this output:
Quote:120
216
169
Traceback (most recent call last):
File "emulator.py", line 19, in <module>
main ()
File "emulator.py", line 16, in main
cpu.process_instructions ()
File "/home/leopoldo/cpu.py", line 17, in process_instructions
instruction.process ()
File "/home/leopoldo/instruction.py", line 14, in process
print ("Identifier Byte: {}".format(self.identity_byte))
AttributeError: 'Instruction' object has no attribute 'identity_byte'

Then I created this self contained example to show it better:

import argparse
from abc import ABCMeta, abstractmethod

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()

HEADER_SIZE = 16
KB_SIZE = 16384

class ROM (object) :
	def __init__ (self, rom_bytes): 
		self.header = rom_bytes [0:HEADER_SIZE]
		self.num_prg_blocks = self.header [4]
		self.data_bytes = rom_bytes [HEADER_SIZE:HEADER_SIZE + (16 + KB_SIZE * self.num_prg_blocks)]
		self.total_size = 16 + KB_SIZE * self.num_prg_blocks

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

from rom import ROM
from instruction import Instruction

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 () 
I do not know why its complaining about the lack of the 'identity_byte' attribute, which is clearly present. So what I am doing wrong/ Thanks for the support
(Sep-29-2019, 11:48 PM)colt Wrote: [ -> ]I do not know why its complaining about the lack of the 'identity_byte' attribute, which is clearly present.

Looks pretty not present to me. You need to assign to self.identity_byte on line 8.
(Sep-30-2019, 12:30 AM)ichabod801 Wrote: [ -> ]
(Sep-29-2019, 11:48 PM)colt Wrote: [ -> ]I do not know why its complaining about the lack of the 'identity_byte' attribute, which is clearly present.

Looks pretty not present to me. You need to assign to self.identity_byte on line 8.

True. Nonetheless, the problem persists (same error message).
I cannot replicate your problem. I can't run all of your code, since I don't have ROM data or anything. But, if I load your classes and do this:

    instruction = Instruction(b'X')
    instruction.process()
    instruction.process2()
It works fine. Can you simplify your code to a more runnable snippet that replicates your problem?
Note that __metaclass_ on line 5 is missing one underscore
(Oct-06-2019, 01:59 AM)ichabod801 Wrote: [ -> ]I cannot replicate your problem. I can't run all of your code, since I don't have ROM data or anything. But, if I load your classes and do this:

    instruction = Instruction(b'X')
    instruction.process()
    instruction.process2()
It works fine. Can you simplify your code to a more runnable snippet that replicates your problem?

I tried with
instruction = Instruction(b'X')
and received:
Quote: File "test.py", line 55
instruction = Instruction (b 'X')
^
SyntaxError: invalid syntax

The "^" is showing after the 'X' here, not in the beggining of instruction
and to answer your question, unfortunately I don't. I need the data that it is contained on the file and don't know how to replace it.
Output:
>>> b'X' 'X' >>> b 'X' File "<stdin>", line 1 b 'X' ^ SyntaxError: invalid syntax
Well, if you can't come up with a smaller version of your code that shows the error, I can't help you.