Hello, everyone. How are you doing?
I'll show you how we can Python Assign functions to variables. I'll show you how to assign functions to variables.
Now suppose we have a named hello function , all we have to do is print the word hello.
You know that to call this function, you will type the name of the function, followed by a set of parentheses.
<span style="color:var(--syntax-text-color)"><span style="color:var(--syntax-text-color)"><code>hello() </code></span></span>
This prints the word "hello", and the parentheses after the function name are the part of the calling function. If you want to remove that set of parentheses later, we won't actually call the function.
For simplicity, we will omit from these examples Main functions.
Function memory address?
Now with Python, python treats almost everything as objects, including functions. So I want to show you something. If I want to print my function name, Hello, what will be displayed is the memory address of this function.
<span style="color:var(--syntax-text-color)"><span style="color:var(--syntax-text-color)"><code><span style="color:var(--syntax-error-color)">>>></span> <span style="color:var(--syntax-declaration-color)">def</span> <span style="color:var(--syntax-name-color)">hello</span><span style="color:var(--syntax-text-color)">():</span> <span style="color:var(--syntax-text-color)">...</span> <span style="color:var(--syntax-declaration-color)">print</span><span style="color:var(--syntax-text-color)">(</span><span style="color:var(--syntax-string-color)">"hello"</span><span style="color:var(--syntax-text-color)">)</span> <span style="color:var(--syntax-text-color)">...</span> <span style="color:var(--syntax-error-color)">>>></span> <span style="color:var(--syntax-text-color)">hello</span> <span style="color:var(--syntax-error-color)"><</span><span style="color:var(--syntax-text-color)">function</span> <span style="color:var(--syntax-text-color)">hello</span> <span style="color:var(--syntax-text-color)">at</span> <span style="color:var(--syntax-literal-color)">0x7f21d6d4e3a0</span><span style="color:var(--syntax-error-color)">></span> <span style="color:var(--syntax-error-color)">>>></span> </code></span></span>
This is the memory address of this function in my computer memory. It is hexadecimal, just like the street address, such as 123 fake street. This is the address of this function in my computer memory. Every time I run this program, this number will change. You can see it here.
One thing we can do now is that we can assign this address to a variable, assuming that hi is equal to hello, and make sure you don't add that set of parentheses later, because then you will call the hello function and return the result to hi.
<span style="color:var(--syntax-text-color)"><span style="color:var(--syntax-text-color)"><code><span style="color:var(--syntax-error-color)">>>></span> <span style="color:var(--syntax-text-color)">hi</span> <span style="color:var(--syntax-error-color)">=</span> <span style="color:var(--syntax-text-color)">hello</span> <span style="color:var(--syntax-error-color)">>>></span> <span style="color:var(--syntax-text-color)">hi</span> <span style="color:var(--syntax-error-color)"><</span><span style="color:var(--syntax-text-color)">function</span> <span style="color:var(--syntax-text-color)">hello</span> <span style="color:var(--syntax-text-color)">at</span> <span style="color:var(--syntax-literal-color)">0x7f21d6d4e3a0</span><span style="color:var(--syntax-error-color)">></span> <span style="color:var(--syntax-error-color)">>>></span> </code></span></span>
So hi equals Hello without parentheses. If I want to print hi well, the addresses of hello and hi will be at the same memory address, and both numbers are now the same.
The above function uses Single return , but this also applies to Multiple returns . So it will return two memory addresses.
<span style="color:var(--syntax-text-color)"><span style="color:var(--syntax-text-color)"><code><span style="color:var(--syntax-error-color)">>>></span> <span style="color:var(--syntax-declaration-color)">def</span> <span style="color:var(--syntax-name-color)">hello</span><span style="color:var(--syntax-text-color)">():</span> <span style="color:var(--syntax-text-color)">...</span> <span style="color:var(--syntax-declaration-color)">print</span><span style="color:var(--syntax-text-color)">(</span><span style="color:var(--syntax-string-color)">'hello'</span><span style="color:var(--syntax-text-color)">)</span> <span style="color:var(--syntax-text-color)">...</span> <span style="color:var(--syntax-error-color)">>>></span> <span style="color:var(--syntax-declaration-color)">def</span> <span style="color:var(--syntax-name-color)">time</span><span style="color:var(--syntax-text-color)">():</span> <span style="color:var(--syntax-text-color)">...</span> <span style="color:var(--syntax-declaration-color)">print</span><span style="color:var(--syntax-text-color)">(</span><span style="color:var(--syntax-string-color)">'14:00'</span><span style="color:var(--syntax-text-color)">)</span> <span style="color:var(--syntax-text-color)">...</span> <span style="color:var(--syntax-error-color)">>>></span> <span style="color:var(--syntax-text-color)">hi</span><span style="color:var(--syntax-text-color)">,</span><span style="color:var(--syntax-text-color)">t</span> <span style="color:var(--syntax-error-color)">=</span> <span style="color:var(--syntax-text-color)">hello</span><span style="color:var(--syntax-text-color)">,</span> <span style="color:var(--syntax-text-color)">time</span> <span style="color:var(--syntax-error-color)">>>></span> <span style="color:var(--syntax-text-color)">hi</span> <span style="color:var(--syntax-error-color)"><</span><span style="color:var(--syntax-text-color)">function</span> <span style="color:var(--syntax-text-color)">hello</span> <span style="color:var(--syntax-text-color)">at</span> <span style="color:var(--syntax-literal-color)">0x7f3db682f3a0</span><span style="color:var(--syntax-error-color)">></span> <span style="color:var(--syntax-error-color)">>>></span> <span style="color:var(--syntax-text-color)">t</span> <span style="color:var(--syntax-error-color)"><</span><span style="color:var(--syntax-text-color)">function</span> <span style="color:var(--syntax-text-color)">time</span> <span style="color:var(--syntax-text-color)">at</span> <span style="color:var(--syntax-literal-color)">0x7f3db682f430</span><span style="color:var(--syntax-error-color)">></span> <span style="color:var(--syntax-error-color)">>>></span> </code></span></span>
Call function
You can imagine what would happen if I called the hi function after we allocated the memory address of hello to hi, so all we have to do is call the Hello function.
<span style="color:var(--syntax-text-color)"><span style="color:var(--syntax-text-color)"><code>>>> hi() hello >>> </code></span></span>
Even if we list the hi function we want to call, even if it doesn't exist, so it's like this Hello function has two names. You can use hello or you can use hi. That's because we will
The memory address of hello is allocated to the variable of Hi, so we can treat hi as a function.
It's a bit like this function has two aliases. If you are interested in Python and want to learn more about Python and AIoT, solve testing problems, and get started guide to help you solve the puzzles encountered in learning python, we have technical experts here. If you are looking for a job, have just come out of school, or have worked, but often feel that there are many difficulties, feel that you are not proficient enough in Python, and want to continue learning. If you want to change careers, you are afraid that you won't learn, you can join us and get the latest interview materials of Python manufacturers and python crawlers, artificial intelligence and learning materials! WeChat official account [Python base] waiting for you to play Austrian.