Python中小错误 之 object() takes no parameters
1 # python 构造函数的使用方法 2 # by 桑榆 3 4 class Test: 5 def _init_(self,name): 6 self.name = name 7 print("%s Welcome to python!" % self.name) 8 9 test = Test('sangyu')
报错如下:
1 Traceback (most recent call last): 2 File "D:/Code/Python/First_test/helloworld.py", line 9, in <module> 3 test = Test('sangyu') 4 TypeError: object() takes no parameters
错误原因:
__init__()构造函数的左右下划线都是两个,只用了一个,导致错误。
修改后:
# python 构造函数的使用方法 # by 桑榆 class Test: def __init__(self,name): self.name = name print("%s Welcome to python!" % self.name) test = Test('sangyu') D:\SoftWare\Python\python.exe D:/Code/Python/First_test/helloworld.py sangyu Welcome to python! Process finished with exit code 0

浙公网安备 33010602011771号