简体   繁体   中英

regarding variable argument in python

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'

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?")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM