Noob Saibot
Noob Saibot

Reputation: 4749

Python: Extract data from memory location

I recently stumbled on this kickass python extension package, Brian Hears that will solve all my coding issues. Problem is, some of the functions return memory addresses instead of expected results. For example:

>>> Parameterize(source, 256, 128)

Out[1]: <Parameterize.Parameterize at 0xda445f8>

I've never seen this before (and don't know its proper name); however, the internet tells me that it's a representation of the memory address of where my result is stored.

I'm really just interested in the result itself. How does one usually go about extracting the actual data from the address in python, or rather the numpy array that the function should (or at least i think it should) return?

Thanks in advance.

EDIT: Added name and link of package

Upvotes: 1

Views: 4574

Answers (3)

tacaswell
tacaswell

Reputation: 87356

It is returning an object. You should do

 p = Parameterize(source, 256, 128)
 res = p.usefull_attribute

and then get your results from the object attributes/properties. You can use python's self-documentation (dir(p), help(p) (as pointed out in other answers + comments)) to get python to tell you what attributes/methods your object has.

What it is printing out is the default string representation of your object, this is it's type and location.

Upvotes: 2

jsbueno
jsbueno

Reputation: 110146

Although it is possible that a very thin Python wrapper, written with ctypes, to some library does actually returns you memory pointers, that does not seen to be the case. The representation on the likes of <Parameterize.Parameterize at 0xda445f8> as you have, is the standard string representation for Python objects.

Even though it actually means a memory address, that number has no use in Python, but to work as an id for your object. (You get hold of it wit "id(object)" ).

To find-out how to use the module you are using, since you are on the interactive prompt, make use of the help and dir introspection builtins to find out what attributes and methods are available on your Parameterize object:

>>> p =Parameterize(source, 256, 128)
>>> p
Out[1]: <Parameterize.Parameterize at 0xda445f8>
>>> dir(p)
>>> help(p)

Upvotes: 2

spinus
spinus

Reputation: 5765

If libarary which you are using is written in C (or C++) and if functions return "popular" (int, str, etc) types you can be interested in ctypes module (boost.python, swing) to wrap C calls with python types. Then you can use this library as python's one. Of course you have to do conversion (you have to define type) with ctypes. For complex structures you probably have to do it on your own like tcaswell said.

Upvotes: 1

Related Questions