You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何定义Person类构造方法后仍报错TypeError: Person() takes no arguments?

问题原因及解决办法

你犯了一个拼写错误:Python类的构造函数应该是__init__(双下划线+init+双下划线),但你写成了__int__。

__int__是Python中用于将对象转换为整数的魔术方法,并非初始化实例的构造函数。由于你没有定义正确的__init__方法,Python会使用默认的无参数构造函数,因此当你传递name和job参数创建Person实例时,就会抛出TypeError: Person() takes no arguments的错误。

修正后的代码如下:

class Person:
    def __init__(self, name="Guido", job="Admin"):
        self.name = name
        self.job = job

此时再执行guido = Person(name='Guido', job='Sales')就能正常创建实例了。

内容的提问来源于stack exchange,提问作者Giselle Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 11:19:50