'Python Function and closure

def html_tag(tag):

    def wrap_text(msg):
        print(f'{tag}  {msg}  {tag}')

    return wrap_text

html_tag('Hi')
print(html_tag)
check_h1 = html_tag('Hi')
print(check_h1)

CONSOLE RETURNS

<function html_tag at 0x108492b00>
<function html_tag.<locals>.wrap_text at 0x1085b0280>

SO I KNOW THAT FIRST LINE OF CONSOLE FUNCTION WAS STORED IN COMPUTER'S MEMORY, BUT WHAT HAPPENED TO THAT 'Hi' ARGUMENT IN html_tag FUNCTION. ALSO IN 2ND LINE OF CONSOLE I GUESS 'Hi' WAS STORED AS ARGUMENT OF tag PARAMETER, BUT WHY THAT DID NOT OCCUR WITH html_tag('Hi')?



Solution 1:[1]

The difference is that once you are referencing the wrap_text function and once the html_tag function.
With the use of

html_tag('Hi')

you do call the function and receive the wrap_text functon, you do however never store the returend result. if you change

html_tag('Hi')
print(html_tag)

to

my_html_tag = html_tag('Hi')
print(my_html_tag)

you would get the expected result.
When you simply call

print(html_tag)

you tell python to print the html_tag function, which is not influenced by you calling it earlier via

html_tag('Hi')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 seven_seas