2016-11-30 122 views
0
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 

任何人都可以请帮我看看上面的代码错误吗?

回答

1

当在python中创建类时,所有嵌套的函数都必须具有'self'参数。

class hive: 
    def abc(self, str): 
     name = str 
     return name + ' welcome' 
+0

obj.abc(字符串)类似于hive.abc(obj,string)。 –