how to compare objects by memory address forcedly.

Just like is in Python, there’re some use cases requiring comparison by memory address, no matter whether the object is mutable or not.

=== doesn’t satisfy the demand for it compares immutable data in bit level.

It’s equivalent to getting the memory address of an object.

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.

3 Likes

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).

Nop. That’s NOT what is is in python either.

Quoting python doc:

The ‘ is ’ operator compares the identity of two objects; the id() function returns an integer representing its identity.

It’s only cpython’s implementation detail that:

id(x) is the memory address where x is stored.

In particular, pypy implements an id and is very similar to julia’s and AFAICT this is only a difference in the implementation level and is not part of the python semantics.

You’re absolutely right, this is a mistake that I ignored the existence of stack usage in Julia runtime…

Anyway there is a use case:

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.

That’s still not a valid/defined thing to do. There’s no significance in the address.

If it’s about an optimization, then the === should be already doing that.

2 Likes