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
  Can we access instance variable of parent class in child class using inheritance akdube 3 233 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  "Class already defined" while using typings. DreamingInsanity 0 369 Aug-19-2020, 10:43 AM
Last Post: DreamingInsanity
  Issue referencing new instance from other class nanok66 3 454 Jul-31-2020, 02:07 AM
Last Post: nanok66
  Class variable / instance variable ifigazsi 9 864 Jul-28-2020, 11:40 AM
Last Post: buran
  python library not defined in user defined function johnEmScott 2 531 May-30-2020, 04:14 AM
Last Post: DT2000
  how to add class instance attributes from list 999masks 2 685 Jul-22-2019, 07:59 AM
Last Post: 999masks
  saving (in text or binary) an object under a defined class cai0824 3 901 May-12-2019, 08:55 AM
Last Post: snippsat
  "not defined" error in function referencing a class Exsul 2 1,284 Mar-27-2019, 11:59 PM
Last Post: Exsul
  Trying to set an instance variable to current value of a class variable ScottDiesing 3 974 Feb-15-2019, 03:12 PM
Last Post: ScottDiesing
  instance of a class in different files DionisiO 2 728 Jan-21-2019, 09:38 PM
Last Post: DionisiO

Forum Jump:

User Panel Messages

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