Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change float('inf') representation?

I want to change float(‘inf’) representation -> ∞, can u help me with it? I try to use this, but it didn’t work

class Inf(float('inf')):
    def __repr__(self) -> str:
        return '∞'

inf=Inf()

those, when I use print() I want to see ‘∞’ instead ‘inf’

I want result like in

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from sympy import oo

but get it with oop

>Solution :

drop the ('inf') in class inheritance. You inherit from classes, not their instances. Then in __repr__ check if your float is infinity.

class FloatWithGlyphInf(float):
    def __repr__(self) -> str:
        if self == float('inf'):
            return '∞'
        else:
            return super().__repr__()

print(FloatWithGlyphInf('inf')) # ∞
print(FloatWithGlyphInf('0'))   # 0.0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading