Python Forum

Full Version: Python complains that class instance is not defined
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello. I have this code line:

instruction = Instruction (byte)
that generates the following error message:

Quote:Traceback (most recent call last):
File "emulator.py", line 29, in <module>
main ()
File "emulator.py", line 21, in main
instruction = Instruction (byte)
NameError: global name 'Instruction' is not defined

Instruction code it is:

class Instruction (object):
	def __init___(self, identifier_byte):
		#type: (byte) ->
		self.identifier_byte = identifier_byte
So, what I am fogetting? Thanks for the input.
Where is the class statement relative to the line causing the error? It looks like the error line is in a function called main. If the class definition is not in the same file before main is called, that could cause the error.
(Sep-14-2019, 12:54 AM)ichabod801 Wrote: [ -> ]Where is the class statement relative to the line causing the error? It looks like the error line is in a function called main. If the class definition is not in the same file before main is called, that could cause the error.

Thanks, that was the issue (solved with an import).

Nonetheless,now I am receiving a:

Quote:Traceback (most recent call last):
File "emulator.py", line 30, in <module>
main ()
File "emulator.py", line 22, in main
instruction = Instruction (byte)
TypeError: object() takes no parameters


and line 22 it is the same! (since I added an import):
instruction = Instruction (byte)
from the class constructor:

def __init___(self, identifier_byte):
		#type: (byte) ->
		self.identifier_byte = identifier_byte
it seems that it takes a parameter for me.
You have three underscores after 'init', not two, so it's not overriding the actual initializer from object.