repr or str of jitclass object

0 views
Skip to first unread message

Michael Boyle

unread,
Feb 2, 2016, 4:56:50 PM2/2/16
to Numba Public Discussion - Public
I really like the new jitclass decorator.  Not knowing how things work under the hood, it seems like it should be easy-ish to implement `__repr__` and `__str__` methods.  For example, say I create some object `Pair`, which just holds a pair of floats.  When I'm playing in ipython, I'd like to be able to print the Pair object, and see those two floats.  Is this a reasonable possibility?

Stanley Seibert

unread,
Feb 2, 2016, 5:02:57 PM2/2/16
to Numba Public Discussion - Public
Yes, we definitely want to add this capability.  More broadly, we want to allow there to be non-JIT methods on a JIT class, which would be needed for things like __repr__ and __str__ to work.


On Tue, Feb 2, 2016 at 3:56 PM, Michael Boyle <michael.ol...@gmail.com> wrote:
I really like the new jitclass decorator.  Not knowing how things work under the hood, it seems like it should be easy-ish to implement `__repr__` and `__str__` methods.  For example, say I create some object `Pair`, which just holds a pair of floats.  When I'm playing in ipython, I'd like to be able to print the Pair object, and see those two floats.  Is this a reasonable possibility?

--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users...@continuum.io.
To post to this group, send email to numba...@continuum.io.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/numba-users/343ca76e-0305-4893-9d3e-82d3ff6c6bb6%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Michael Boyle

unread,
Feb 12, 2016, 6:34:39 PM2/12/16
to Numba Public Discussion - Public
As a workaround for displaying them nicely, there's at least the following option in ipython, which automatically calls the function you give it for the type you specify.  Here, I've made up an example `MyObject` class, which has some defining attributes `w`, `x`, `y`, and `z`:

ip = get_ipython()
text_formatter
=ip.display_formatter.formatters['text/plain']
def _print_jitted_object(arg, p, cycle):
   
"""Pretty print a jitclass instance"""
    p
.text("{0}({1}, {2}, {3}, {4})".format(type(arg).__name__, arg.w, arg.x, arg.y, arg.z))
text_formatter
.for_type('numba.jitclass.boxing.MyClass', _print_jitted_object);


Reply all
Reply to author
Forward
0 new messages