Home > Net >  Passing multiple strings to a function and appending to list
Passing multiple strings to a function and appending to list

Time:01-15

class Dog:
   tricks = []
   def __init__(self,name):
     self.name=name
   def add_tricks(self, trick ):
     self.tricks.extend(trick)

I keep getting the error that add_tricks only accepts 1 positional arguments but 3 were given. I tried passing **kwargs to the function but that didn't work either. How do I pass multiple strings to the function?

CodePudding user response:

For *args

class Dog:
   tricks = []
   def __init__(self,name):
     self.name=name
   def add_tricks(self, *trick):
     self.tricks.extend(trick)

OR For **kwargs

class Dog:
   tricks = []
   def __init__(self,name):
     self.name=name
   def add_tricks(self, **trick):
     self.tricks.extend(trick)

Or if you want to give string in both types like (a="Hello","byy")

class Dog:
   tricks = []
   def __init__(self,name):
     self.name=name
   def add_tricks(self, *trick1,**trick2):
     self.tricks.extend(trick)
     self.tricks.extend(trick2)
  •  Tags:  
  • Related