Hi all,

I'm wondering if it is possible to convert an object reference to a string and vica versa?

Thanks

Recommended Answers

All 5 Replies

Not exactly sure what it is you're looking for. But many built-in objects can have the str() function applied to them with little problem.

If it's a custom class or similar you may need to look into the Pickling module. I don't know if you plan to use the string for something other than storing it on your FS; however here's the documentation.

I'm using a 3D program called blender, for some reason gameobjects objects can only store number type variables, bool and strings. Pickling would not good in this case as it serialises the whole object, and the contents will be no longer be up to date. I've noticed if you use the print command on an object refernce you get the Class type and memory address of it, I was wondering if there was a way to store that in a string, and then turn it back into an object reference when needed.

Thanks in advance.

Have a looksee here:

http://docs.python.org/lib/built-in-funcs.html

The id() function and maybe more, didn't have much time to look for more... Maybe the int of the reference is enough, you can definitely turn that into a string and recover your int or whatever you want....


There might also be 'getattr':
http://diveintopython.org/power_of_introspection/getattr.html

Let me know if it's useful...

However, if you would like to retrieve the object with that reference later... I don't quite know how that would work, I read something of a module mxTools , and also found this thread in a forum which looks like the same problem you have:

http://mail.python.org/pipermail/python-list/2002-September/162983.html

That certainly is usefull, I can store the reference a int instead of a string, is there anyway of converting that int back into a reference?

A solution is to keep a dictionary of the objects you reference. The dictionary would have the object's id's as keys and the object as values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.