Define a class with optional argument in Python

前端 未结 3 679
迷失自我
迷失自我 2021-02-14 14:43
class class1():
    def setdata(self,value1, value2):
    self.data = value1+value2
def display(self):
    print(self.data)

For the above code, when I

相关标签:
3条回答
  • 2021-02-14 15:21

    Try this please:

    def setdata(self, value1, value2 = '000'):
       Your code here
    
    0 讨论(0)
  • 2021-02-14 15:40
    class class1:    
        def setdata(self,value1, value2=456):
            self.data = value1+value2
        def display(self):
            print(self.data)
    

    may be this solves your problem

    0 讨论(0)
  • 2021-02-14 15:41

    Try defining a default argument, like so.

    class class1():
        def setdata(self,value1, value2='000'):
            ...
    
    0 讨论(0)
提交回复
热议问题