Python Forum
My class has name error message
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My class has name error message
#1
Okay this is the first time I've created a Python class and I'm running into a problem. The debugger says NameError: Instrument is not defined.

I don't see the problem. Here is my code:

class Instrument:
    
    def __init__ (self):
        self.type = "Guitar"
        
    def main():
        instrument = Instrument()
        
        print("My instrument is", instrument)
        
    main()
Reply
#2
Is that your actual indentation? It looks like you are creating an instance of the class from the main method within the class. I could see doing that in certain cases, but here it seems very odd. Then you call that method from within the class definition. Definitely odd.

I wouldn't use type as an attribute name, as it can get confused with the built-in type function.

And please post the full text of the error you are getting.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Okay I modified my code and so far it is working. I moved the instance creation out of the main function and into the class definition.

Now all I need to do is figure out how to print "My instrument is guitar".

Here is what I got now:
class Instrument:
    
    def __init__ (self):
        self.type = "Guitar"
        my_instrument = Instrument()
        
    def get_instrument(self):
        return self.type
    
     
    def main():
        print("My instrument is")
          
    main()
Reply
#4
Some problem here,also in basic understanding concepts of class(oop).
Don't use type word as it used bye Python.

If think of it so should information about instrument come in from outside,and not be hard coded in class.
This way can eg all band members can use same class.
class Instrument:
    def __init__(self, instrument):
        self.instrument = instrument

    def description(self):
        return f'My instrument is {self.instrument}'
Usage:
>>> player_1 = Instrument('Gutiar')
>>> player_2 = Instrument('Drums')
>>> player_1.instrument
'Gutiar'
>>> player_2.instrument
'Drums'
>>> 
>>> player_1.description()
'My instrument is Gutiar'
>>> player_2.description()
'My instrument is Drums'
A quick look a at __str__ and __repr__,which is useful many times to have in a class.
__str__ could replace description() method.
class Instrument:
    def __init__(self, instrument):
        self.instrument = instrument

    def __str__(self):
        return f'My instrument is {self.instrument}'

    def __repr__(self):
         return f'Instrument({self.instrument!r})'
Usage:
>>> player_1 = Instrument('Gutiar')
>>> player_2 = Instrument('Drums')
>>> 
>>> # print call __str__
>>> print(player_1)
My instrument is Gutiar
>>> 
>>> # only object show __repr__
>>> player_2
Instrument('Drums')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error message about iid from RandomizedSearchCV Visiting 2 932 Aug-17-2023, 07:53 PM
Last Post: Visiting
  Another Error message. the_jl_zone 2 942 Mar-06-2023, 10:23 PM
Last Post: the_jl_zone
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,675 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  understanding error message krlosbatist 1 1,856 Oct-24-2021, 08:34 PM
Last Post: Gribouillis
  Error message pybits 1 35,942 May-29-2021, 10:26 AM
Last Post: snippsat
  f-string error message not understood Skaperen 4 3,267 Mar-16-2021, 07:59 PM
Last Post: Skaperen
  Overwhelmed with error message using pandas drop() EmmaRaponi 1 2,300 Feb-18-2021, 07:31 PM
Last Post: buran
  Winning/Losing Message Error in Text based Game kdr87 2 2,927 Dec-14-2020, 12:25 AM
Last Post: bowlofred
  error in class non_name092 1 1,859 Sep-02-2020, 05:42 PM
Last Post: bowlofred
  Don't understand error message Milfredo 2 1,994 Aug-24-2020, 05:00 PM
Last Post: Milfredo

Forum Jump:

User Panel Messages

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