Bottom Page

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()
Quote
#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

Quote
#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()
Quote
#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')
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to get valid error message while invoking REST API through Python gollupraveen 0 55 Oct-04-2019, 07:15 PM
Last Post: gollupraveen
  Project, Reading Data from a spreadsheet. Error message!! Shafla 1 84 Sep-27-2019, 10:44 AM
Last Post: buran
  object is not subscriptable... Error Message? Shafla 3 183 Sep-25-2019, 12:27 AM
Last Post: buran
  Openpyxl error message. Help! Shafla 6 115 Sep-24-2019, 07:25 PM
Last Post: Shafla
  clueless about the error message findbikash 2 124 Sep-19-2019, 01:10 PM
Last Post: findbikash
  error message on program launch DJPY1175 0 253 Jun-10-2019, 08:40 AM
Last Post: DJPY1175
  Error in implementing multithreading in a class srm 2 208 May-16-2019, 03:54 PM
Last Post: Yoriz
  Getting error message for indentation Shafla 5 263 May-07-2019, 08:56 PM
Last Post: Yoriz
  "not defined" error in function referencing a class Exsul 2 434 Mar-27-2019, 11:59 PM
Last Post: Exsul
  error message with module Pepper887 3 310 Mar-16-2019, 06:19 PM
Last Post: Yoriz

Forum Jump:


Users browsing this thread: 1 Guest(s)