SHUBHAM TANDAN SHUBHAM TANDAN - 3 days ago 5
Python Question

printing output using object in python

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()



I want to print name with welcome string concatenated with it,but I can't be able to do so and I am getting the error below :


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



Can anyone please help me find the error in the above code ?

Answer

When making classes in python, all nested functions must have the 'self' argument.

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