简体   繁体   中英

Creating a Class error - Object() takes no parameters

I'm learning Python from a book titled, "Python Crash Course" by Eric Matthes.

I'm creating my first Class and using the code from the book. However, when I run the code, it gives me an error, "object() takes no parameters."

Using Sublime Text - Python 2.7

I've triple checked the code from the book and I'm certain I got it accurately. I'm not sure what else I can try.

```

class Dog(object):
    """A simple attempt to model a dog."""

    def _init_(self, name, age):
        """Initialize name and age attributes."""
        self.name = name
        self.age = age

    def sit(self):
        """Simulate a dog sitting in response to a command."""
        print(self.name.title() + " is now sitting.")

    def roll_over(self):
        """Simulate rollign over in response to a command."""
        print(self.name.title() + " rolled over!")



my_dog = Dog('willie', 6)

print("My dog's name is " + my_dog.name.title() + ".")
print("My dog is " + str(my_dog.age) + " years old.")

```

The output of the code is supposed to be: My dog's name is Willie. My dog is 6 years old.

Instead, I'm receiving this error message in Sublime Text:

"Traceback (most recent call last):
File "/Users/recklessfire13/Library/Application Support/Sublime Text 3/Packages/User/python_work/dog.py", line 19, in <module>
self.my_dog = Dog('willie', 6)
TypeError: object() takes no parameters
[Finished in 0.1s with exit code 1]"

replace

def _init_(self, name, age):

with

def __init__(self, name, age):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM