0

It says

    sarah = Athlete('Sarah Sweeney', '2002-6-17', ['2:58', '2.58', '1.56'])
TypeError: object() takes no parameters

What's wrong? Thank you. It is the example in Head First Python

class Athlete:
    def _init_(self, a_name, a_dob=None, a_times=[]):
        self.name = a_name
        self.dob = a_dob
        self.times = a_times

sarah = Athlete('Sarah Sweeney', '2002-6-17', ['2:58', '2.58', '1.56'])
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
caosz
  • 47
  • 9
  • 1
    Change `_init_` to `__init__` – Tyler Nov 11 '13 at 15:50
  • 5
    You *probably* do **not** want to use a mutable list value `[]` for a default, see ["Least Astonishment" in Python: The Mutable Default Argument](http://stackoverflow.com/q/1132941) – Martijn Pieters Nov 11 '13 at 15:50
  • I hope you've read: http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument – Bakuriu Nov 11 '13 at 15:51

2 Answers2

4

That's because you don't overload magic __init__ method of the object class. Instead of that you define a new method called _init_ which has no special meaning to Python (special methods in Python should be surrounded with double underscores).

As far as your class does not overload __init__, a default one (inherited from object) gets called every time you instantiate the class. And that default one takes no arguments.

See also: Special (magic) methods in Python, Where is the Python documentation for the special methods? (__init__, __new__, __len__, ...)

Community
  • 1
  • 1
Eldar Abusalimov
  • 24,387
  • 4
  • 67
  • 71
3

You've misspelt __init__ (note the double underscores at the start and the end).

NPE
  • 486,780
  • 108
  • 951
  • 1,012