How to get garbage values in python
Question: This question may not have any practical value, this is just out of curiosity.
In C/C++ when you declare a variable like below:

    
    
    int c;
    

variable `c` will have some garbage value in it. As per my understanding
python doesn't have variables as in C/C++ but it has name-bindings with
objects. So, according to me there is no way to get garbage as you always
first create the object and then assign it a tag.

Garbage value actually is just whatever was stored at the location variable
refers. But, I want to know if there is some way to get garbage values in
python. In other words is there some way to access memory location without
first initializing it. In my view it should not be possible and I appreciate
it if this is by design. But I am just curious.

I am still learning both python and C++ and am not an expert so forgive me if
this is a stupid question.


Answer: This things usually depend on the interpreter that you are using. For example
with CPython, you could do something like in [this
answer](http://stackoverflow.com/questions/121396/accessing-object-memory-
address), or basically use C libraries like [this other
answer](http://stackoverflow.com/questions/8250625/access-memory-address-in-
python) suggests.

> to access memory location without first initializing it

Could be done with something like this,

    
    
    from ctypes import string_at
    from sys import getsizeof
    from binascii import hexlify
    # create a dummy object, just to get a memory location
    a = 0x7fff
    # and use it as a pivoting point to access other locations
    offset = 100
    print(hexlify(string_at(id(a)+offset, getsizeof(a))))
    

Needless to say this kind of things have its risks, but are an interesting
experiment.

