Python Forum
Object cannot find its attribute
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Object cannot find its attribute
#1
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
Reply
#2
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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).
Reply
#4
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?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Note that __metaclass_ on line 5 is missing one underscore
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(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.
Reply
#7
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,672 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,865 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 730 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,329 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,590 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  WebDriver' object has no attribute 'find_element_by_css_selector rickadams 3 5,900 Sep-19-2022, 06:11 PM
Last Post: Larz60+
  'dict_items' object has no attribute 'sort' Calli 6 4,474 Jul-29-2022, 09:19 PM
Last Post: Gribouillis
  AttributeError: 'numpy.ndarray' object has no attribute 'load' hobbyist 8 7,099 Jul-06-2022, 10:55 AM
Last Post: deanhystad
  AttributeError: 'numpy.int32' object has no attribute 'split' rf_kartal 6 4,352 Jun-24-2022, 08:37 AM
Last Post: Anushka00
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 4,871 Apr-27-2022, 09:27 AM
Last Post: Anldra12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020