question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot overwrite the __str__ method in jitclass

See original GitHub issue

Hello,

I am not able to overwrite the string representation of a jitclass. I do not know if this is a wanted behaviour though.

from numba.experimental import jitclass
from numba import float64

spec = [("alpha", float64)]


@jitclass(spec)
class Foo:
    def __init__(self, alpha):
        self.alpha = alpha
    
    def __str__(self):
        return "Class Foo"


foo = Foo(1.)

print(str(foo))

Produces:

<numba.experimental.jitclass.boxing.Foo object at 0xS0M3wh3r3>

Expected:

Class Foo

It seems that the __str__() method is skipped entirely, in fact the following does not produce any ZeroDivisionError.

...
    def __str__(self):
        return 0 ** (-2)

Tested on:

Platform: Linux-5.15.0-52-generic-x86_64-with-glibc2.35
Python version: 3.9.7 (default, Sep 20 2022, 16:21:01) 
[GCC 11.2.0]

Numba version: 0.55.2

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
stuartarchibaldcommented, Oct 24, 2022

Thank you @stuartarchibald! After updating to 0.56.3, the __str__ method works!

My bad, I did not notice it from the Release notes

No problem, glad it is working now.

Next question: what about __repr__? Any possibility for it to be implemented in a future release? It’d be really nice to have it for usage within Jupyter Notebook.

I suggest opening a feature request if it is important to you. I think it’s technically possible to support it.

0reactions
stuartarchibaldcommented, Oct 24, 2022

Done!

Ref. #8533

Great, many thanks. I’ll close this issue as resolved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to update a jitclass variable with its string name by ...
Here is a solution I came up with. I first wrote a function that return a list of the variable names I can...
Read more >
Implementation of __repr__ method within jitclass #8533
Hello, Numba 0.56.0 has introduced support for many builtins for the numba.experimental.jitclass type. Would it be possible to add __repr__, ...
Read more >
Compiling Python classes with @jitclass - Numba
Calling static methods as class attributes is only supported outside of the class definition (i.e. you can't call Bag.add() from within another method...
Read more >
Compiling Python classes with @jitclass
We call the resulting class object a jitclass. All methods of a jitclass are compiled into nopython functions. The data of a jitclass...
Read more >
How do I nest dict-of-jitclass - Support - Numba Discussion
I suspect that this is a bug, Numba's trying to serialize something for the purposes of box/unboxing the data from the CPython interpreter...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found