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
  understanding error message krlosbatist 1 35 37 minutes ago
Last Post: Gribouillis
  Error message pybits 1 553 May-29-2021, 10:26 AM
Last Post: snippsat
  f-string error message not understood Skaperen 4 841 Mar-16-2021, 07:59 PM
Last Post: Skaperen
  Overwhelmed with error message using pandas drop() EmmaRaponi 1 712 Feb-18-2021, 07:31 PM
Last Post: buran
  Winning/Losing Message Error in Text based Game kdr87 2 832 Dec-14-2020, 12:25 AM
Last Post: bowlofred
  error in class non_name092 1 782 Sep-02-2020, 05:42 PM
Last Post: bowlofred
  Don't understand error message Milfredo 2 843 Aug-24-2020, 05:00 PM
Last Post: Milfredo
  what to do about this error message deric23 0 836 May-15-2020, 02:49 PM
Last Post: deric23
  pip error message Marijn 1 878 Apr-21-2020, 12:05 PM
Last Post: snippsat
  Adding second message to simple loop error PythonGainz 2 926 Apr-06-2020, 11:55 AM
Last Post: PythonGainz

Forum Jump:

User Panel Messages

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