[英]Creating a Class error - Object() takes no parameters
我正在从Eric Matthes的书《 Python Crash Course》中学习Python。
我正在创建我的第一堂课,并使用书中的代码。 但是,当我运行代码时,它给我一个错误,“ object()不带参数”。
使用Sublime文本-Python 2.7
我已经对本书中的代码进行了三遍检查,我确定我正确地获得了它。 我不确定还能尝试什么。
```
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.")
```
该代码的输出应为:我的狗叫威利。 我的狗6岁了。
相反,我在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]"
更换
def _init_(self, name, age):
与
def __init__(self, name, age):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.