Python Forum
NameError: NameError: global name 'BPLInstruction' is not defined - 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: NameError: NameError: global name 'BPLInstruction' is not defined (/thread-21922.html)



NameError: NameError: global name 'BPLInstruction' is not defined - colt - Oct-20-2019

Hello I am receiving these messages when running my code:

Error:
File "/home/leopoldo/cpu.py", line 28, in process_instructions instruction = self.identify_instruction (byte) File "/home/leopoldo/cpu.py", line 18, in identify_instruction instruction = BPLInstruction (byte) NameError: global name 'BPLInstruction' is not defined
The code:

def process_instructions (self):
		counter=0
		for byte in self.rom.data_bytes:
			byte = self.rom.get_byte (self.pc)
			instruction = self.identify_instruction (byte)
			if instruction is None:
				raise Exception ("Instruction not found")
			instruction.process ()
			print ("self.pc, antes: ", self.pc)
			self.pc+= instruction.instruction_length
			print ("counter: ", counter)
			print ("self.pc, depois: ", self.pc)
			print ("")
			counter = counter + 1
def identify_instruction (self, byte):
		if byte==0x78:	
			instruction = LDAInstruction (byte)
		elif byte==0xD8:
			instruction = SEIInstruction (byte)
		elif byte==0xA9:
			instruction = CLDInstruction (byte)
		elif byte==0x10:
			instruction = BPLInstruction (byte)
		else:
			instruction = None
		return instruction
class LDAInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: LDAInstruction:",hex (self.identity_byte), self.identity_byte )
	instruction_length = 2

class SEIInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: SEIInstruction:",hex (self.identity_byte), self.identity_byte)
	instruction_length = 1

class CLDInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: CLDInstruction:",hex (self.identity_byte), self.identity_byte)
	instruction_length = 1

class BPLinstruction (Instruction):
	def process (self):
		print ("Identifier Byte: BPLinstruction:",hex (self.identity_byte), self.identity_byte)
	instruction_length = 1
I can't identify the source of trouble because I see no difference between the code related to BPL, whether is its class definition or its elif clause on "identify_instruction" to the others instruction's code.

So I don't see what is not defined there, especially when python does not complain about any other instruction. Thanks for the help.


RE: NameError: NameError: global name 'BPLInstruction' is not defined - Larz60+ - Oct-21-2019

in class definition, name is BPLinstruction (small i)
when instantiated you use capital I


RE: NameError: NameError: global name 'BPLInstruction' is not defined - colt - Oct-23-2019

(Oct-21-2019, 12:51 AM)Larz60+ Wrote: in class definition, name is BPLinstruction (small i)
when instantiated you use capital I

Thanks. I did changed it to
class BPLInstruction (Instruction):
instead of
class BPLinstruction (Instruction):
Nonetheless, the error message continued, exactly as before. What else can be?


RE: NameError: NameError: global name 'BPLInstruction' is not defined - Larz60+ - Oct-23-2019

it's line 18!
print ("Identifier Byte: BPLinstruction:",hex (self.identity_byte), self.identity_byte)


RE: NameError: NameError: global name 'BPLInstruction' is not defined - colt - Oct-25-2019

(Oct-23-2019, 11:29 PM)Larz60+ Wrote: it's line 18!
print ("Identifier Byte: BPLinstruction:",hex (self.identity_byte), self.identity_byte)

Between the quotes? Well, I changed it but the error remains.


RE: NameError: NameError: global name 'BPLInstruction' is not defined - Larz60+ - Oct-26-2019

show your full code


RE: NameError: NameError: global name 'BPLInstruction' is not defined - colt - Oct-27-2019

I somewhat was able to fix it through the correction of a logical bug.

The code that fix it is:

        if byte==0x78:	
			instruction = SEIInstruction (byte)
		elif byte==0xD8:
			instruction = CLDInstruction (byte)
		elif byte==0xA9:
			instruction = LDAInstruction (byte)
and not

def identify_instruction (self, byte):
		if byte==0x78:	
			instruction = LDAInstruction (byte)
		elif byte==0xD8:
			instruction = SEIInstruction (byte)
		elif byte==0xA9:
			instruction = CLDInstruction (byte)
However I added code for a new instruction with

class STAabsInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: STAabsInstruction:",hex (self.identity_byte), self.identity_byte )
	instruction_length = 3
and code to deal with it
elif byte==0x8D:
			instruction = STAabsInstruction (byte)
and now I am receiving
Error:
File "/home/leopoldo/cpu.py", line 32, in process_instructions instruction = self.identify_instruction (byte) File "/home/leopoldo/cpu.py", line 20, in identify_instruction instruction = STAabsInstruction (byte) NameError: global name 'STAabsInstruction' is not defined
Full code for the two classes that use Instructions:

from rom import ROM
from instruction import LDAInstruction, SEIInstruction, CLDInstruction

class CPU (object):
	def __init__(self, rom_bytes):
		self.registers = []
		self.rom = ROM (rom_bytes)
		self.pc = 0		

	def identify_instruction (self, byte):
		if byte==0x78:	
			instruction = SEIInstruction (byte)
		elif byte==0xD8:
			instruction = CLDInstruction (byte)
		elif byte==0xA9:
			instruction = LDAInstruction (byte)
		elif byte==0x10:
			instruction = BPLInstruction (byte)
		elif byte==0x8D:
			instruction = STAabsInstruction (byte)
		else:
			instruction = None
		return instruction
		

	def process_instructions (self):
		counter=0
		for byte in self.rom.data_bytes:
			byte = self.rom.get_byte (self.pc)
			print ("self.pc, antes: ", self.pc)
			print ("byte: ", hex (byte), byte)
			instruction = self.identify_instruction (byte)
			if instruction is None:
				raise Exception ("Instruction not found")
			instruction.process ()
			self.pc+= instruction.instruction_length
			print ("counter: ", counter)
			print ("self.pc, depois: ", self.pc)
			print ("")
			if self.pc>=5:
				break
			
from abc import ABCMeta, abstractmethod, abstractproperty

class Instruction (object):
	__metaclass_ = ABCMeta
		
	def __init__ (self,identification_byte):
		self.identity_byte = identification_byte

	@abstractproperty
	def instruction_length (self):
		return 1

	@abstractmethod
	def process (self):
		pass
		
class SEIInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: SEIInstruction:",hex (self.identity_byte), self.identity_byte)
	instruction_length = 1

class CLDInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: CLDInstruction:",hex (self.identity_byte), self.identity_byte)
	instruction_length = 1

class LDAInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: LDAInstruction:",hex (self.identity_byte), self.identity_byte )
	instruction_length = 2

class STAabsInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: STAabsInstruction:",hex (self.identity_byte), self.identity_byte )
	instruction_length = 3



RE: NameError: NameError: global name 'BPLInstruction' is not defined - Larz60+ - Oct-27-2019

I assume these classes are in separate files, and each named the same as the class that they hold? (you don't show the module names)
on line 2 of cpu.py, you don't import STAabsInstruction, thus the error on line 20.