Python Forum
AttributeError: 'Register' object has no attribute 'bit_7' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: AttributeError: 'Register' object has no attribute 'bit_7' (/thread-23142.html)



AttributeError: 'Register' object has no attribute 'bit_7' - colt - Dec-12-2019

Hello. I have this simple class
class Register (object):
	def init (self):
		self.bit_0 = False
		self.bit_1 = False
		self.bit_2 = True
		self.bit_3 = False
		self.bit_4 = False
		self.bit_5 = True
		self.bit_6 = True
		self.bit_7 = False
		self.bit_10 = False
I have also this other class that instantiates a instance of the above class:

from register import Register

class Registers (object):
	def __init__ (self):
		self.pc = 0	
		self.stack_pointer = int (0xFD)
		self.accumulator = 0
		self.index_x  = 0
		self.index_y  = 0
		self.status = Register ()
Finally, I have this method that belongs to a third class:

def set_bit_2_SR (self,registers): 		
		print ("registers.status before setting bit 2: ", registers.status.bit_2)
		registers.status.bit_2 = True
		print ("registers.status after setting bit 2: ", registers.status.bit_2)
When executing my program I end receiving:

Quote:Traceback (most recent call last):
File "emulator.py", line 19, in <module>
main ()
File "emulator.py", line 16, in main
cpu.process_instructions ()
File "/media/34GB/demos/PythonNes/cpu.py", line 59, in process_instructions
instruction.process (self.registers,self.ram,self.rom)
File "/media/34GB/demos/PythonNes/instruction.py", line 223, in process
self.set_bit_2_SR (registers)
File "/media/34GB/demos/PythonNes/instruction.py", line 29, in set_bit_2_SR
print ("registers.status before setting bit 2: ", registers.status.bit_2)
AttributeError: 'Register' object has no attribute 'bit_2'

However, a curious fact is that if I add a
print ("registers.status before setting bit 2: ", registers.status.bit_7)
to the method "set_bit_2_SR", just before the line:
		print ("registers.status before setting bit 2: ", registers.status.bit_2)
the error message starts to be about the bit_7 :

Quote:AttributeError: 'Register' object has no attribute 'bit_7'

If I replace all the mentions to "bit_2" on the "set_bit_2" method to references to accumulator (that belongs to "Registers" and not to "Register") like
	def set_bit_2_SR (self,registers): 		
		print ("registers.status before setting bit 2: ", registers.accumulator)
		registers.accumulator = 15
		print ("registers.status after setting bit 2: ", registers.accumulator)
the error message vanishes. Yet this other method that belongs to the third class:
	def set_bit_7_SR (self,registers):
		print ("registers.status before setting bit 7: ", registers.status.bit_7)
		registers.status.bit_7 = True
		print ("registers.status after setting bit 7: ", registers.status.bit_7)
keeps generating an error message similar to the previous ones. So the issue here is specifically connected to "Register". Nonetheless, I have absolutely no idea why it is no finding these simple attributes. So I would appreciate suggestions about the "why" behind it.


RE: AttributeError: 'Register' object has no attribute 'bit_7' - micseydel - Dec-12-2019

Register has an init method when it needs an __init__ method.

EDIT: you may want to look into dataclasses by the way.