Simple Function Question
Remco Gerlich
scarblac-spamtrap at pino.selwerd.nl
Thu Apr 27 10:28:28 EDT 2000
Akira Kiyomiya wrote in comp.lang.python:
> Hi, Could you explain this in dummy's language?
>
>
> # Return a function that returns its argument incremented by 'n'
> def make_incrementer(n):
> def increment(x, n=n):
> return x+n
> return increment
>
> add1 = make_incrementer(1)
> print add1(3) # This prints '4'
Let's try simpler versions. Do you understand the following?
# Return a function that returns its argument incremented by 3
def make_incrementer():
def increment(x):
return x+3
return increment
Now this one:
# Return a function that adds its two arguments together, or by default adds 3
def make_incrementer():
def increment(x, y=3):
return x+y
return increment
And finally, the version where you can give another default value:
# Return a function that adds n to its argument:
def make_incrementer(n):
def increment(x, y=n):
return x+y
return increment
The last one is equivalent to your version, except here one variable is
called y instead of n. Maybe it was confusing that the previous one used 'n'
for two different variables.
--
Remco Gerlich, scarblac at pino.selwerd.nl
'Oook?'
More information about the Python-list
mailing list