Python assign functions and memory address

Tags: python
Published: 2021-01-12 13:09:52


Hello what's going on everybody? 

I'm going to show you guys how we can assign a function to a variable in [Python](https://python.org). I will show you all how we can assign a function to a variable.

Now let's say we have a [function](https://pythonspot.com/functions/) 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()
```

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](https://python-commandments.org/python-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.

```python
>>> def hello():
...     print("hello")
... 
>>> hello
<function hello at 0x7f21d6d4e3a0>
>>> 
```

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](https://dev-to-uploads.s3.amazonaws.com/i/it33qqw0rouwli37fad3.jpeg)

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.

```python
>>> hi = hello
>>> hi
<function hello at 0x7f21d6d4e3a0>
>>> 
```

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](https://pythonprogramminglanguage.com/functions/) but this works with [multiple return](https://pythonbasics.org/multiple-return/) too. So what it will return are two memory addresses.

```python
>>> def hello():
...    print('hello')
... 
>>> def time():
...    print('14:00')
... 
>>> hi,t = hello, time
>>> hi
<function hello at 0x7f3db682f3a0>
>>> t
<function time at 0x7f3db682f430>
>>> 
```

## 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
>>> 
```

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.