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

Python expressions in f-strings do not return same result as str() conversion for subclasses?

For example, we override the __str__ method in Decimal then call it two different ways

from decimal import Decimal
class D(Decimal):
    def __str__(self):
        return super().normalize().__str__()
num = D('1.0')
print(f"{num}")
print(f"{str(num)}")

outputs

1.0
1

But they should both output 1. It seems like the first print is calling Decimal.__str__ instead of D.__str__ for some reason. Why does this happen and how do I fix the behavior?

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

>Solution :

You’ll need to override __format__ too (see the f-string PEP, search for __format__).

from decimal import Decimal


class D(Decimal):
    def __str__(self):
        return super().normalize().__str__()

    def __format__(self, format_spec):
        return super().normalize().__format__(format_spec)


num = D('1.0')
print(f"{num}")
print(f"{str(num)}")

prints out

1
1
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