class hive:
def abc(str):
name = str
return name + ' welcome'
def main():
obj = hive()
print('enter name')
string = input()
print(obj.abc(string))
if __name__ == "__main__": main()
我想打印带有连接的欢迎字符串的名称,但我无法这样做,我收到以下错误:
C:\Users\SHUBHAM TANDAN\Desktop\python>class.py
enter name
shubham
Traceback (most recent call last):
File "C:\Users\SHUBHAM TANDAN\Desktop\python\class.py", line 12, in <module>
if __name__ == "__main__": main()
File "C:\Users\SHUBHAM TANDAN\Desktop\python\class.py", line 10, in main
print(obj.abc(string))
TypeError: abc() takes 1 positional argument but 2 were given
有人可以帮我找到上面代码中的错误吗?
答案 0 :(得分:1)
在python中创建类时,所有嵌套函数都必须具有&#39; self&#39;参数。
class hive:
def abc(self, str):
name = str
return name + ' welcome'