0

Below is the function example.

def testfun(self,total, count, fruitname):
    self.totalvalue = total + count
    print "fruitname"

 self.testfun(10,5,"apple")

output:

  apple

Now I i need to print extra fruit name using testfun

so when i call like this:

 self.testfun(10,5,"apple","orange")

Expected out:

apple
orange

Is it possible achieve above output using same function "testfun" or I need to write two different functions.

Why i am asking is i have big function i need to call twice and for second time calling i need to print one extra input .

Any suggestions would be highly appreciated

Bittu
  • 31
  • 2
  • 9

1 Answers1

2

You can specify a *args argument to a function. This *args is a catch-all argument for everything specified after the first two.

>>> def foo(x, y, *args):
...    print(x, y)
...    for arg in args:
...        print(arg)
... 
>>> 
>>> foo(10, 5, 'apple')
(10, 5)
apple
>>> foo(10, 5, 'apple', 'orange')
(10, 5)
apple
orange

This answer explains the concept in nice detail.

cs95
  • 379,657
  • 97
  • 704
  • 746