3

I came across this interesting code

# Define echo
def echo(n):
    """Return the inner_echo function."""

    # Define inner_echo
    def inner_echo(word1):
        """Concatenate n copies of word1."""
        echo_word = word1 * n
        return echo_word

    # Return inner_echo
    return inner_echo


# Call echo: twice
twice = echo(2)

# Call echo: thrice
thrice = echo(3)

# Call twice() and thrice() then print
print(twice('hello'), thrice('hello'))

output :

hellohello hellohellohello

But I'm not able to understand how it is working since twice and thrice functions is calling the function echo and providing the value n, how is the value of word1 is passed?

Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
  • First echo function is initialized, like class if you already familiar with class. This construct is similar you initialize main function, than you can pass value to inner methods. Eg. in JavaScript there is no proper classes (no new type like String is created) - they are actually functions containing other functions. – Zydnar Nov 27 '17 at 05:03
  • 1
    What you are looking at is called a closure. When I first came across the concept I had trouble wrapping my head around it, but once you know the term you can search for it and read a few things to gain an understanding. A lot of languages have them, here is a link that does a decent job of explaining it.: https://stackoverflow.com/a/7464475/1468125 – Gavin Nov 27 '17 at 05:06

4 Answers4

1

The echo function returns another function, that has n set to whatever you pass it. When you call inner_echo (ie, the return of echo) it keeps the scope that it was given when created.

In your example, twice is created using echo(2) which returns the inner_echo function which, in it's scope, n is set to 2.

Likewise, thrice, created by echo(3) creates a new version of inner_echo, where n is set to 3.

Remember how echo returns a function? When you call twice or thrice you are calling the function that echo returned - ie, you are not calling echo at all. Therefore calling twice is calling inner_echo and that is how word is being populated.

Shadow
  • 8,749
  • 4
  • 47
  • 57
1

echo is a function that takes a parameter n and returns another function that closes over the supplied value of n and also takes an argument word1.

In other words, calling echo(n) for some n returns a function which is then called with twice(word1)

The flow is essentially.

echo = function (n) -> function (word1) with closure (n) -> word1 repeated n times

twice = echo (2) -> function (word1) with closure (n = 2) -> word1 repeated 2 times

twice('hello') -> 'hello' repeated 2 times

I have described it in the above manner because, AFAICT Python does not have a syntax for expressing the types of functions.

Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84
0

When you call echo with a value 2 in twice = echo(2) , it stores the value 2 internally. So when we call twice('hello'), it remembers the value of n and prints it that many times. When you pass 3 to the same function as thrice = echo(3) , it stores the value 3 internally.

So, basically it is creating instances with different values of n. I hope you understood.

Try passing it twice = echo(2) and then twice = echo(3) , In this case it will update the value of n.

Sumit S Chawla
  • 3,180
  • 1
  • 14
  • 33
0

The above example is the example of Function Closures

Better you can refer this link Function Closure In Python

Sanket
  • 744
  • 7
  • 22
  • Don't just post a link to the docs, quote something relevant for posterity. – Aluan Haddad Nov 27 '17 at 05:06
  • My purpose to post a link was, first he will try to understand the concept of function closure and the try to debug above example, it become easy for him and good for him @AluanHaddad – Sanket Nov 27 '17 at 05:11
  • Good idea, but that would be a comment and not an answer is all. This doesn't really add anything as an answer. But adding links to docs in your answer is great. – Aluan Haddad Nov 27 '17 at 05:11