0

Every time I try to print the location of a function, it gives different value. but location of a variable printed using id() gives out the same value every time. I am using python v3. Below is the code.

def func():
   pass
    
x = 10
print('ID for variable x: {}'.format(hex(id(x))))
print('ID for function: {}'.format(func))

and below is it's output,

PS C:\users\xyz\python> python new.py

ID for variable x: 0x6e7fa580

ID for function: <function func at 0x000002C1AA5A3E18>

PS C:\users\xyz\python> python new.py

ID for variable x: 0x6e7fa580

ID for function: <function func at 0x0000026F92C13E18>

PS C:\users\xyz\python> python new.py

ID for variable x: 0x6e7fa580

ID for function: <function func at 0x00000268DA153E18>

  • @Goion Python is not a compiled language. – Klaus D. May 23 '21 at 06:48
  • It is not but the concept of memory assignment isn't wildly different. @KlausD. The takeaway should be that there are many factors that determine how the memory is allocated. There are other applications as well on OS which are competing for the memory. So it makes sense that you don't always get same memory allocation. – Yoshikage Kira May 23 '21 at 06:49
  • [Memory management in python](https://towardsdatascience.com/memory-management-in-python-6bea0c8aecc9) This is more focused on how python does memory under the hood. – Yoshikage Kira May 23 '21 at 06:53
  • I am new to programming, learning it as a hobby. I wrongly assumed the answer to this would be straight forward. but I am sure the resources you guys pointed out would help in the right direction. Thanks!! – Shailesh Joshi May 23 '21 at 06:57
  • @ShaileshJoshi With python you don't need to worry much about how the memory is allocated. Focus on learning the data structures and algorithms. – Yoshikage Kira May 23 '21 at 06:59

0 Answers0