为何实例化Robot类时出现Type Error: Robot() takes no argument错误?
TypeError: Robot() takes no arguments Issue Hey there, let's get this error sorted out right away— I can spot the exact problem here.
The Root Cause
Your Robot class's constructor method has a tiny but critical typo: you wrote __init(self, name, color, weight) instead of the required __init__(self, name, color, weight).
In Python, the class constructor must be named __init__ (with two underscores at both the start and end). When you use only one trailing underscore (__init), Python doesn't recognize this as the class's proper constructor. Instead, it uses the default no-argument constructor that comes with all classes. That's why passing arguments like "johh","Red", 40 to Robot() throws the "takes no arguments" error.
Corrected Code
Here's the fixed version of your code, with the proper constructor name and a small readability tweak for the output:
class Robot: def __init__(self,name, color, weight): # Fixed: added the second trailing underscore self.name = name self.color = color self.weight = weight def self_introduce(self): print(f"hello my name is {self.name}, my eye colour is {self.color} and my weight is {self.weight} ") r1 = Robot("johh","Red", 40) r2 = Robot("murry","Blue", 45) r1.self_introduce() r2.self_introduce()
内容的提问来源于stack exchange,提问作者Shishir Acharya

