Erlport: Instantiation of python class and calling methods on the object

Hello everybody,
so I played with erlport and it works well for me on a module base (calling functions).
I tried to instanciate a custom python class to get an object, but I cannot call any methods on this object later.
I am not sure, if a completely understand how erlport works in the background, so maybe it not possible to “hold” the instance in erlang and later call methods on this instance.
Does anybody know if this should work with erlport? Some hint would be welcome.

[EDIT]
Basically what I do is this:

Python:

class Pyclass:

def __init__(self):
    print("pyClass __init__")
    self.value = 3

def get_value(self):
    return self.value

Erlang:

   {ok, Python} = python:start_link([{python_path, "/path/"}, {python, "python3"}]),
   {'$erlport.opaque', python, Object} = python:call(Python, pyclass, 'Pyclass', []),
   python:call(Python, pyclass, 'Pyclass.get_value', [Object]).

what I get when running the code:
"'bytes' object has no attribute 'value'"

so self does not represent my python object, that’s for sure

Thanks, alex

What is it if you print it out (or repr it on the python side)?

1 Like

Hi, OvermindDL1,

printed out Object in erlang:
<<128,2,99,112,121,99,108,97,115,115,10,80,121,99,108,97,115,115,10,113,0,41,129,113,1,125,113,2,88,5,0,0,0,118,97,108,117,101,113,3,75,3,115,98,46>>
self in python:
b'\x80\x02cpyclass\nPyclass\nq\x00)\x81q\x01}q\x02X\x05\x00\x00\x00valueq\x03K\x03sb.'

some binary
I think I have to encode/decode, but I have no clue how to do that.
Unfortunately in the examples I have seen so far, only static methods are called, so maybe this is not working.

1 Like

Hmm, I’m unsure, might need to ask on their issue tracker, the docs seem to imply that should work…

I found the answer in a lfe lib called py, they use some extra python encoders/decoders for this (besides other things).
I could not get it to work, so I created pythra which uses their extra python modules and has the relevant lfe code translated to erlang.

Cheers

2 Likes

Wait what? An lfe library? Lisp Flavoured Erlang is awesome!

Oh yes it is, I did not really had a hard time reading it, although seen for the first time.
I translated the code mainly because I could not make it work for me and I do not want the extra lfe code.

1 Like