3

I'm a newbie to Python, with a Java background. I came across the following function definition

def S(seq,i=0):
    print i
    if i==len(seq): 
        return 0    
    return S(seq,i+1)+seq[i]

What exactly does the i=0 do here, is it re-initialised to 0 each time? Because I notice that the value of i is incremented .

NPE
  • 486,780
  • 108
  • 951
  • 1,012
seeker
  • 6,841
  • 24
  • 64
  • 100

1 Answers1

8

It provides the default value for the second argument.

The function can be called with either one or two arguments. If it's called with one, the second argument, i, defaults to zero.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • so that means that in this case `seq` will be a mandatory argument? – seeker May 09 '12 at 15:04
  • 3
    @KodeSeeker: Yes, `seq` would be a mandatory argument. Also, Python default arguments often trip up programmers coming from other languages. Make sure you read [Default Parameter Values in Python](http://effbot.org/zone/default-values.htm). – Steven Rumbalski May 09 '12 at 15:11