Touch a Python object from Elixir

ErlPort allows you to work with Python and Ruby code from Elixir.

However, there was no description about the class in Documentation, so I actually tried it.

From the conclusion, it behaves a little counter-intuitive.

Preparation

$ mix new python

python/mix.exs


defmodule Python.Mixfile do
  # ...

  defp deps do
    [
      {:erlport, github: "hdima/erlport"}
    ]
  end
end

python/sample.py


class Sample(object):
    def __init__(self):
        print("Sample.__init__")
        self.message = "Hello, world!"

    def print_message(self):
        print(self.message)

    def set_message(self, msg):
        self.message = msg

Run

Start the python process with : python.start () and call the function with : python.call ().

$ iex -S mix
iex(7)> {:ok, python} = :python.start([python_path: '.'])
iex(8)> obj = python |> :python.call(:sample, :Sample, [])
Sample.__init__
iex(9)> python  |> :python.call(:sample, :"Sample.print_message", [obj])
Hello, world!

There is no problem so far. Next, try calling Sample.set_message ().

iex(13)> python |> :python.call(:sample, :"Sample.set_message", [obj, "Hello, ErlPort!"])
iex(14)> python  |> :python.call(:sample, :"Sample.print_message", [obj])
Hello, world!

that? The message hasn't changed.

If you try to return self withSample.set_message ()and let the caller receive it,

python/sample.py


class Sample(object):
    # ...

    def set_message(self, msg):
        self.message = msg
        return self
iex(15)> {:ok, python} = :python.start([python_path: '.'])
iex(16)> obj = python |> :python.call(:sample, :Sample, [])
Sample.__init__
iex(18)> obj = python |> :python.call(:sample, :"Sample.set_message", [obj, "Hello, ErlPort!"])
iex(19)> python  |> :python.call(:sample, :"Sample.print_message", [obj])
Hello, ErlPort!
:undefined

This time the message has changed. It behaves as if self is an immutable object.

Recommended Posts

Touch a Python object from Elixir
Touch MySQL from Python 3
How to generate a Python object from JSON
Create a datetime object from a string in Python (Python 3.3)
Call a Python function from p5.js.
python / Make a dict from a list.
Send a message from Python to Slack
Create a JSON object mapper in Python
# 5 [python3] Extract characters from a character string
Create a deb file from a python package
Generate a class from a string in Python
Use Django from a local Python script
Manipulate BigQuery tables from a Python client
Call a command from Python (Windows version)
Operate Sakura's cloud object storage from Python
API explanation to touch mastodon from python
Python built-in object
sql from python
Python built-in object
MeCab from Python
Edit Excel from Python to create a PivotTable
How to open a web browser from python
How to create a function object from a string
Create a C array from a Python> Excel sheet
A memorandum of calling Python from Common Lisp
Create a New Todoist Task from Python Script
"Python Kit" that calls a Python script from Swift
Create a decision tree from 0 with Python (1. Overview)
Call a Python script from Embedded Python in C ++ / C ++
Run a Python file from html using Django
Read line by line from a file with Python
Run a python script from excel (using xlwings)
Insert an object inside a string in Python
Generate and output plantuml object diagram from Python object
Extract data from a web page with Python
A * algorithm (Python edition)
Reading CSV data from DSX object storage Python code
[Python] Take a screenshot
Operate Filemaker from Python
Use fluentd from python
Create a Python module
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Make a copy of a Google Drive file from Python
Receive dictionary data from a Python program in AppleScript
A python lambda expression ...
Python from or import
Use MySQL from Python
I tried running python etc. from a bat file
Run python from excel
Install python from source
Execute command from Python
[ev3dev × Python] Touch sensor
Python points from the perspective of a C programmer
Python beginners touch Pytorch (3)
Daemonize a Python process
Nothing remains after making a python map object a list
Operate neutron from Python!
From a book that programmers can learn ... (Python): Pointer
Steps from installing Python 3 to creating a Django app