How to override a user-defined method generated by python swig

When python extension is generated by swig, the following `` repr``` is automatically defined in the structure defined in C.

<swig_mymodule.mytype; proxy of <Swig Object of type 'struct mytype *' at 0x7f6c74210a20> >

I'd like to do something because it's not cool as it is, but I couldn't find a way to control it on the C side (or rather, with swig).

If you do setattr in% python code as shown below, the contents will be output in the python code generated by swig, so it will be overwritten at the time of import.

swig_mymodule.i


%pythoncode %{
import types

def __mytype_str(self):
    return "<Mytype(…)>”

setattr(mytype, '__str__', types.MethodType(__mytype_str, None, mytype))
%}

Recommended Posts

How to override a user-defined method generated by python swig
How to save a table scraped by python to csv
How to write a Python class
[Python] How to make a list of character strings character by character
How to use the __call__ method in a Python class
[Python] How to force a method of a subclass to do something specific
[Python] How to make a class iterable
[Python] How to convert a 2D list to a 1D list
[Python] How to invert a character string
How to get a stacktrace in python
How to run a Maya Python script
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
[Python] How to read a csv file (read_csv method of pandas module)
How to sort by specifying a column in the Python Numpy array.
[python] How to display list elements side by side
How to read a CSV file with Python 2/3
How to create a Python virtual environment (venv)
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to generate a Python object from JSON
How to add a Python module search path
How to erase the characters output by Python
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
[Python] How to sort instances by instance variables
[python] How to sort by the Nth Mth element of a multidimensional array
How to install a Python library that can be used by pharmaceutical companies
[Python] How to easily drop a child process started by multiprocess from another process
How to install Python
How to install python
How to convert / restore a string with [] in python
[Python] How to draw a line graph with Matplotlib
How to set up a Python environment using pyenv
[Python] How to expand variables in a character string
How to build a Django (python) environment on docker
[Python] How to write a docstring that conforms to PEP8
[Python] Created a method to convert radix in 1 second
[Python] How to create a 2D histogram with Matplotlib
How to execute a command using subprocess in Python
How to build a Python environment on amazon linux 2
[Python] How to call a c function from python (ctypes)
How to create a kubernetes pod from python code
[Python] How to draw a scatter plot with Matplotlib
How to execute a schedule by specifying the Python time zone and execution frequency
How to publish GitHub Pages with Pelican, a static HTML generator made by Python
How to create a Python 3.6.0 environment by putting pyenv on Amazon Linux and Ubuntu
[2020.8 latest] How to install Python
How to install NPI + send a message to line with python
How to install Python [Windows]
python3: How to use bottle (2)
How to switch the configuration file to be read by Python
How to build a new python virtual environment on Ubuntu
How to slice a block multiple array from a multiple array in Python
How to convert an array to a dictionary with Python [Application]
How to run a Python file at a Windows 10 command prompt
How to run a Python program from within a shell script
How to shuffle a part of a Python list (at random.shuffle)
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to launch AWS Batch from a python client app
How to import a file anywhere you like in Python