0

I am at a strong beginner level in Python. Recently, I came up with a question that had put me in a deep thought:

Is there any way through which I may get to know whether a given memory address (in integer or string) is occupied or free for use in Python?

This might have been possible with languages which are designed keeping in mind a proper memory model like c and c++, but when it comes to such a dynamically typed programming language will it be possible?

While doing some research, I came to know about the id() function. So, is there any way to leverage this to find out the answer to this problem?

P0intMaN
  • 128
  • 1
  • 14
  • 1
    Python is a memory managed language (note, static typing vs dynamic typing is not a relevant distinction here), the whole point is that you don't worry about this. The language itself exposes no methods of dealing with memory. The question, "memory location available for use" doesn't make much sense in the context of Python. `id` is a function that returns a unique integer for the lifetime of an object, the fact that it is the address of the PyObject header in CPython is an implementation detail, it is only incidentally that, in other implementations, it can be different. – juanpa.arrivillaga Aug 02 '21 at 06:17
  • 1
    Does this answer your question? [Access memory address in python](https://stackoverflow.com/questions/8250625/access-memory-address-in-python) – Grismar Aug 02 '21 at 06:24
  • Okay, so Its an implementation of CPython there. Nicely explained @juanpa.arrivillaga, I got where I was wrong. Python doesn't pre assign a memory location when you declare a variable. I got this sort of confusion because I migrated from c. Thanks to all. – P0intMaN Aug 02 '21 at 06:39
  • 2
    In Python you don't *declare* variables at all. They come into existence and get bound to a name when you assign to that name. Assignment can be implicit, as with arguments to a function. They disappear some time after there are no names bound to them anymore, because you have bound the name to something else, or because the name has gone out of scope. – BoarGules Aug 02 '21 at 10:11
  • Was just reading about it @BoarGules . Python clears the leftover memory in a process called automatic garbage collection right? In C, the memory assigned used to stay throughout the termination of program (even if there are no names bound to them). Thanks to you too for taking time and explaining. – P0intMaN Aug 02 '21 at 11:40

0 Answers0