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
#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


Messages In This Thread
My class has name error message - by 357mag - Sep-04-2019, 01:29 PM
RE: My class has name error message - by ichabod801 - Sep-04-2019, 01:33 PM
RE: My class has name error message - by 357mag - Sep-04-2019, 02:05 PM
RE: My class has name error message - by snippsat - Sep-04-2019, 03:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Error message about iid from RandomizedSearchCV Visiting 2 1,034 Aug-17-2023, 07:53 PM
Last Post: Visiting
  Another Error message. the_jl_zone 2 987 Mar-06-2023, 10:23 PM
Last Post: the_jl_zone
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,118 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  understanding error message krlosbatist 1 1,918 Oct-24-2021, 08:34 PM
Last Post: Gribouillis
  Error message pybits 1 41,995 May-29-2021, 10:26 AM
Last Post: snippsat
  f-string error message not understood Skaperen 4 3,360 Mar-16-2021, 07:59 PM
Last Post: Skaperen
  Overwhelmed with error message using pandas drop() EmmaRaponi 1 2,379 Feb-18-2021, 07:31 PM
Last Post: buran
  Winning/Losing Message Error in Text based Game kdr87 2 3,008 Dec-14-2020, 12:25 AM
Last Post: bowlofred
  error in class non_name092 1 1,939 Sep-02-2020, 05:42 PM
Last Post: bowlofred
  Don't understand error message Milfredo 2 2,064 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