Python Forum
AttributeError: 'Register' object has no attribute 'bit_7'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: 'Register' object has no attribute 'bit_7'
#1
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.
Reply
#2
Register has an init method when it needs an __init__ method.

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 718 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,529 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,385 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Parallel processing - AttributeError: Can't get attribute 'sktimekmeans' Mohana1983 1 704 Jun-22-2023, 02:33 AM
Last Post: woooee
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,662 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 685 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,211 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,298 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  WebDriver' object has no attribute 'find_element_by_css_selector rickadams 3 5,775 Sep-19-2022, 06:11 PM
Last Post: Larz60+
  Pymodbus Write value to register stsxbel 10 7,886 Aug-18-2022, 01:42 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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