I don’t think this is possible. What about objects allocated only on the stack? Do you want their address on the stack? In which frame? What about objects which are elided completely by the compiler? Should an address be invented for them? What about an object which is bound to a local variable (perhaps copied onto the stack) and also stored inline in an array? What address should be used in that case?
Asking for the “memory address” of something which is not stored on the heap isn’t really a well-formed question. What are you actually trying to do? Perhaps we can find another way to achieve it.
If you really have to compare the address it’d better be significant for you. In general (in particular for immutable objects) this means you would already have that address (e.g. if you are interfacing with julia in C) and comparing the address is then trivial. Otherwise, the question is invalid (i.e. there’s no instance where you don’t already have the address and have to force the address of a possibly immutable object).
What if I want to compare immutable strings by memory address? There is a long story about a significant parsing/lexer technology, whose crucial part is tesing whether several strings are the same object.
I’m sorry to mix up CPy implentation details and Py language specifications. I’ve worked on CPython bytecode optimization for quite a while, and usually I take advantage of memory address to speed it programs if possible. I might use CPython instead of Python, hope my fault wouldnt annoy you.