How is this a valid answer when converting boolean value to a string

Viewed 23

we have a function bool_to_string(flag) and you're supposed to define it so that the bool value passed to the function i.e flag is always returned as a string.

bool_to_string = str

This was a valid answer but I don't understand how it works. I know about lambda but this doesn't appear to be that. Can someone explain to me how it is so short?

edit: like the person below highlighted, our original function was just converting a value to string. That is the same the built-in function str() does so we have just swapped out our original function with str. Simple and clean solution.

1 Answers

The expression creates a new function bool_to_string and assigns it the value of function str. In essence, bool_to_string is an alias for the str function.

In Python, everything is an object you can work with functions in the same way as with any other data type.

Related