Home > other >  what is returning a function?
what is returning a function?

Time:12-31

so, my professor has taught us this and i don't get it. i run the code in jupyter notebook and nothing happens. can someone tell me what is this code supposed to do? it has something to do with returning a function which i don't really understand.

def make_logger(target):
    def logger(data):
        with open(target, 'a') as f:
            f.write(data   '\n')
    return logger

foo_logger = make_logger('foo.txt') #foo.txt will be created if not there already
foo_logger('Hello')
foo_logger('World')

i tried running it in python which gave me a syntax invalid error.

CodePudding user response:

In Python everything is an object. Just like any variables that you define, a function also is an object.

I would highly recommend to read through this beautiful post which goes in length at describing this

In the above program, that you pasted, let us analyze the below line

foo_logger = make_logger('foo.txt')

It is returning logger function as the result.

Now the logger function can in itself take any argument as a data and write it to a file, so when you call the

foo_logger('Hello')
foo_logger('World')

It will then go ahead and append this to the file named foo.txt and you will have the file contents as Hello World

You can check that by doing a cat foo.txt And the function is accurate, doesn't produce any syntax error.

CodePudding user response:

make logger returns a function (logger) that takes an argument and writes it to a the file provided by make_logger's argument. The following lines that call foo_logger pass data to it, and have it write that data to the log file.

CodePudding user response:

First of all the code, the code written by you ran fine and doesn't have any syntax errors.

def make_logger(target):
    def logger(data):
        with open(target, 'a') as f:
            f.write(data   '\n')
    return logger

foo_logger = make_logger('foo.txt') #foo.txt will be created if not there already
foo_logger('Hello')
foo_logger('World')

This program ran, created, and saved the file foo.txt at the same path as the .py file present.

So, It basically ran as follows.

  • This line foo_logger = make_logger('foo.txt') returned a function via function call in which the destination of the file is passed as the target parameter and saved as the variable. So, you can call it later.
  • Yes, you heard it right. We can assign functions as variables in python, pass them arguments inside functions and return them via functions as well.

e.g. Assigning a function as a variable.

def plus_one(number):
    return number   1

add_one = plus_one
add_one(5)

Output:

6
  • This line foo_logger('Hello') Called the function with parameters as input. It writes 'Hello' in the already created foo.txt file and saved it.

  • Again, this line foo_logger('World') was called, and append the foo.txt file with 'World' as text in it.

So, The output of this program saved in file will be

Hello
World

This is the same concept being used in Python Decorators to wrap any function with some additional functionalities to an existing object without modifying its structure. You can understand it better here

  • Related