0
class Song(object):
    def _init_(self, lyrics):
        self.lyrics = lyrics
    def sing_song(self):
        for line in self.lyrics:
            print line
happy_bday = Song("Happy Birthday to you",
            "I might get sued for this",
            "So I'll stop right here")
            
print happy_bday

happy_bday.sing_song()

***This gives me error ***

Traceback (most recent call last):
  File "ex36.py", line 9, in <module>
    "So I'll stop right here")
TypeError: object() takes no parameters

Am I missing something here? Thanks in advance

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
  • This is python 2? If not that should be `print(line)` and `print(happy_bday)` – Henry Ecker Jul 18 '21 at 03:08
  • 3
    Also the function is `__init__` there are _2_ underscores... Also to take variable number of arguments you'll need to use `*` -> `def __init__(self, *lyrics)` – Henry Ecker Jul 18 '21 at 03:09
  • 1
    `__init__` is a special python method, you need two underscores on either side – lemonhead Jul 18 '21 at 03:09
  • 1
    Applicable -> [What __init__ and self do in Python?](https://stackoverflow.com/q/625083/15497888) and [Can a variable number of arguments be passed to a function?](https://stackoverflow.com/q/919680/15497888) – Henry Ecker Jul 18 '21 at 03:10

1 Answers1

2

You need to use

def __init__(self, lyrics):

instead of

def _init_(self, lyrics):

Inside your class. Also, your __init__ function takes only 1 argument, while you're passing in 3. If you want it to take all your arguments as a list, you could use

def __init__(self, *lyrics):

And lyrics will be a list of all your arguments you passed in.

iamkneel
  • 1,303
  • 8
  • 12