0

i am writing a class in python and in that i am calling a function to display variable argument.In the beginning the function calculates the sum of integers.

When i am calling the function without any class,then it is printing the values but inside the class,it is throwing error.

def displayString(name, msg = "Good morning!"):

   print("Hello",name + ', ' + msg)

displayString("Kate")
displayString("Bruce","How do you do?")

the above is displaying the result but when i am calling the function inside the class below, it is throwing error


class new:


    answer=0
    def __init__(self,f,s):

        self.first=f
        self.second=s

    def display(self):

        print("this is the first ={} and the second={} value".format(self.first,self.second))
        print("the answer is {}".format(self.answer))

    def calculate(self):

        self.answer=self.first+self.second

    def displayString(name, msg = "Good morning!"):
        print("Hello",name + ', ' + msg)


if __name__=="__main__":

    x=new(1,2)
    x.calculate()
    x.display()
    x.displayString("kate")
    x.displayString("micky","How are you?")

TypeError                                 Traceback (most recent call last)
<ipython-input-38-fd51df6426ac> in <module>
     26     x.calculate()
     27     x.display()
---> 28     x.displayString("kate")
     29     x.displayString("micky","How are you?")
     30 

<ipython-input-38-fd51df6426ac> in displayString(name, msg)
     18 
     19     def displayString(name, msg = "Good morning!"):
---> 20         print("Hello",name + ', ' + msg)
     21 
     22 

TypeError: unsupported operand type(s) for +: 'new' and 'str'

1
  • 1
    Inside a class this should be def displayString(self, name, msg = "Good morning!") Commented Sep 8, 2019 at 23:04

1 Answer 1

3

When you put the function inside the class, it becomes a method. Python methods by default require a self parameter. The name self has no special meaning, so when you missed out the the self argument, it passed the same value that it otherwise would have passed to the self parameter into the first argument, name, causing the error when you tried to add the reference to the object (stored in name) to a string: name + ', '

There are two ways to fix this. This most obvious is to add the self parameter:

class new:
    def displayString(self, name, msg = "Good morning!"):
        print("Hello",name + ', ' + msg)


if __name__=="__main__":
    x = new()
    x.displayString("kate")
    x.displayString("micky","How are you?")

The second is to stop python from passing in the self argument by using the staticmethod decorator:

class new:
    @staticmethod
    def displayString(name, msg = "Good morning!"):
        print("Hello",name + ', ' + msg)


if __name__=="__main__":
    x = new()
    x.displayString("kate")
    x.displayString("micky","How are you?")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.