[英]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.