1
votes

I am trying to run the program from the book Learn Python the Hard Way but it's throwing an error. Could you please help me on what I am doing wrong here?

This is the error message I'm getting:

Traceback (most recent call last):
  File "C:\Desktop\Python-testing\My-file.py", line 11, in 
<module>
    "So I'll stop right there"])
TypeError: object() takes no parameters

This is the Python Code:

class Song(object):

  def _init_(self, lyrics):
    self.lyrics=lyrics
  def sing_me_a_song(self):
    for line in self.lyrics:
      print line

happy_bday = Song(["Happy birthday to you",
               "I dont want to get sued",
               "So I'll stop right there"])

bulls_on_parade = Song(["The rally around the family",
                    "with pockets ful of shales"])

happy_bday.sing_me_a_song()
bulls_on_parade.sing_me_a_song()
2

2 Answers

3
votes

In python, the initializer method is named __init__, not _init_ (two underscores instead of one). So the method definition

def _init_(self, lyrics):

just defines an ordinary method, instead of overriding object.__init__. Therefore, when you initialize the class with an argument, object.__init__(['Happy birthday...']) gets called, and that fails.

To fix this problem, write __init__ with 2 underscores at each side (4 total):

def __init__(self, lyrics):
1
votes

Your constructor should be def __init__ rather than def _init_.

The Python interpreter recognizes def _init_ as a normal function and hence, it cannot find a constructor, and hence the error that "object() doesn't take arguments".