python - 类中的方法不带参数

标签 python python-3.x

我是 Python 的新手,我只是想制作一个简单的类来测试它。我在“Patient”类中定义我的方法时使用了 self 参数,但是当我尝试构建时仍然出现“不接受参数错误”。这是我要运行的代码...

class Patient:
    def _init_(self,name,weight=0,LegLen=0):
        self.name = name
        self.weight = weight
        self.LegLen = LegLen


Mark = Patient('Mark')
print(Mark.name)

然后我得到错误... 马克=病人('马克') TypeError: Patient() 没有参数

我做错了什么?

最佳答案

_init_ 应该拼写为 __init__(双下划线)。否则它只是一个方法,而不是构造函数。

关于python - 类中的方法不带参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51791662/

相关文章:

Python:WAITING用户输入,如果10分钟后没有输入,则继续执行程序

python - 运行 python3 -bb 时,为什么 set([b'foo', u'foo']) 失败但 set([b'foo', u'bar']) 成功?

python-3.x - 如何在屏幕上同时创建数千个图像和像素?使用 Python

python - 如何根据上一行和下一行的条件在 Pandas Dataframe 上创建新列?

python - 如何用唯一 ID 替换 Python Pandas 表文本值?

python - 如何解码 python 中的反斜杠转义字符串?

python - 如何在 OS X 上安装 python 3

python - Pandas.mean() 类型错误 : Could not convert to numeric

Python编码列表 'list'对象没有属性 'encode'

python - 使用转换计算数据框中的特定值和聚合结果