2

I have a python set that contains a collection of non-hashable python objects with uniform type which I want to process.

To improve efficiency of my algorithms, I would like to interface using ctypes with an external index implementation that accepts only uint64 as data values.

I was hoping that I could to pass pointer references to the python object into this external library as uint64?

I tried ctypes.cast(ctypes.py_object(my_python_object), ctypes.c_uint64) but am getting ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type.

Also, what about the reverse, getting a reference to a python object as uint64 and turning it into a "real" python object?

10
  • Seriously, what's wrong with using id()? Commented Apr 14, 2016 at 18:32
  • @AnttiHaapala Seriously nothing. :-) Just that I never realised it existed. Any chance of getting a python object from the id() value? If not, I could always convert my set into a dict with the ids as keys. Commented Apr 14, 2016 at 18:36
  • The thing is... If your object is dead, you cannot get it back from the id() - instead you will crash your interperter :D Commented Apr 14, 2016 at 18:37
  • Would tools like Cython or manually writing a small extension module to interface with the C code be an option? Commented Apr 14, 2016 at 18:43
  • What you're doing seems dubious, but you haven't provided enough details to say one way or the other. Anyway, if you're working directly on Python objects using a C library, make sure to load it as a PyDLL instance that holds the GIL when calling functions. Then just set the function's argtypes, with the Python object parameter defined as py_object. The C function will handle this as a uint64_t. Passing the object directly increments the reference count during the call, so there's no danger of the object getting deallocated on another thread. Commented Apr 14, 2016 at 19:20

1 Answer 1

4

Why wouldn't you simply use the id() function in CPython?

>>> x
<object object at 0x7fd2fc742090>
>>> hex(id(x))
'0x7fd2fc742090'

The CPython documentation of id() says that

id(object)

Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

CPython implementation detail: This is the address of the object in memory.


You also need to mess with the reference counts and such, if you're to "convert" this uint64_t of yours back to a Python object. As far as I know, ctypes do not easily let one to increase/decrease the reference counts of Python

Sign up to request clarification or add additional context in comments.

2 Comments

Strictly speaking, this implementation detail could change in the future.
The very similar case of object.__hash__ actually has changed in the past; it no longer just returns id(self). I wouldn't be entirely surprised if they ever decide to change id, for example, to reduce the same kind of bucket collisions that occurred with object.__hash__.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.