Python assign functions and memory address

bluepaperbirds

bluepaperbirds

Posted on January 12, 2021

Python assign functions and memory address

Hello what's going on everybody?

I'm going to show you guys how we can assign a function to a variable in Python. I will show you all how we can assign a function to a variable.

Now let's say we have a function named hello and all we'll do is print the word hello.

You know to call this function, you would type the name of the function followed by a set of parentheses.

hello()
Enter fullscreen mode Exit fullscreen mode

and this will print the word "hello" the set of parentheses that comes after a function's name is the portion that will call the function. If you were to remove that set of parentheses afterwards we would not in fact call that function.

For simplicity, we'll leave out the main function from these examples.

Function memory address?

Now with python python will pretty much treat everything as objects including functions. So there's something i want to show you guys, if i was to print the name of my function hello what will be displayed is the memory address of this function.

>>> def hello():
...     print("hello")
... 
>>> hello
<function hello at 0x7f21d6d4e3a0>
>>> 
Enter fullscreen mode Exit fullscreen mode

This is the memory address of where this function is located within my computer's memory and it's in hexadecimal think of it like a street address such as 123 fake street. This is the address of where this function is within my computer's memory and each time that i run this program this number can change as you can see it here.

computer memory

Now one thing that we could do we could assign this address to a variable let's say hi equals hello and be sure that you're not adding that set of parentheses afterwards because then you would be calling the hello function and returning the result to hi.

>>> hi = hello
>>> hi
<function hello at 0x7f21d6d4e3a0>
>>> 
Enter fullscreen mode Exit fullscreen mode

So hi equals hello no parentheses and if i was to print hi well the address of hello and hi will be at the same memory address both of these numbers are the same now.

The above function is using a single return but this works with multiple return too. So what it will return are two memory addresses.

>>> def hello():
...    print('hello')
... 
>>> def time():
...    print('14:00')
... 
>>> hi,t = hello, time
>>> hi
<function hello at 0x7f3db682f3a0>
>>> t
<function time at 0x7f3db682f430>
>>> 
Enter fullscreen mode Exit fullscreen mode

Call function

What do you imagine would happen if i was to call the hi function after we assign the memory address of hello to hi well then what we'll end up doing is calling the hello function.

>>> hi()
hello
>>> 
Enter fullscreen mode Exit fullscreen mode

Even though we're listing that we would like to call the hi function even though it doesn't exist so it's as if this hello function has two names you, can either use hello or you can use hi. That's because we're assigning the
memory address of hello to this variable of hi, so we could treat hi as a function.

It's kind of like an alias where this function has two names.

đź’– đź’Ş đź™… đźš©
bluepaperbirds
bluepaperbirds

Posted on January 12, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related