As Jake described already, your make_repeater
returns another function with n
being bound to 2
(this is called a closure). So your code is roughly equivalent to:
twice = lambda s: s * 2
print twice('word')
print twice(5)
Which is in turn roughly equivalent to:
def twice(s):
return s * 2
print twice('word')
print twice(5)
Which is in turn roughly equivalent to:
print 'word' * 2
print 5 * 2
So what you actually do is:
- evaluate the expression
'word' * 2
, which results in 'wordword'
(multiplication of strings is defined by Python as repeating the string the given number of times)
- evaluate the expression
5 * 2
, which results in 10
(this should not surprise you)
The fact that your lambda function doesn't care about the type of its argument and dynamically decides at runtime which method of multiplication is correct, is called dynamic typing.