Python Forum
Python complains that class instance is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python complains that class instance is not defined
#1
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.
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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.
Reply
#4
You have three underscores after 'init', not two, so it's not overriding the actual initializer from object.
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
  "Name is not defined" when running a class lil_e 6 3,770 Jan-12-2023, 11:57 PM
Last Post: lil_e
  Access instance of a class Pavel_47 5 2,035 Nov-19-2021, 10:05 AM
Last Post: Gribouillis
  Why built in functions are defined as class? quazirfan 5 2,717 Oct-23-2021, 01:20 PM
Last Post: Gribouillis
  Class Instance angus1964 4 2,407 Jun-22-2021, 08:50 AM
Last Post: angus1964
  Error when refering to class defined in 'main' in an imported module HeRo 2 2,335 Apr-13-2021, 07:22 PM
Last Post: HeRo
  Can we access instance variable of parent class in child class using inheritance akdube 3 13,889 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  "Class already defined" while using typings. DreamingInsanity 0 2,305 Aug-19-2020, 10:43 AM
Last Post: DreamingInsanity
  Issue referencing new instance from other class nanok66 3 2,180 Jul-31-2020, 02:07 AM
Last Post: nanok66
  Class variable / instance variable ifigazsi 9 4,220 Jul-28-2020, 11:40 AM
Last Post: buran
  python library not defined in user defined function johnEmScott 2 3,774 May-30-2020, 04:14 AM
Last Post: DT2000

Forum Jump:

User Panel Messages

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