Re: Print a float value


Stéphane Lozier
 

In the first case you're printing the float, in the second case you're printing a tuple of floats. In both cases the `__str__` function is called, however in the case of the tuple, `__str__` and `__repr__` are the same so it's like calling `__repr__` of each of its items.

>>> class test(object):
...   def __str__(self): return "__str__"
...   def __repr__(self): return "__repr__"
...
>>> print(test())
__str__
>>> print(test(), test())
(__repr__, __repr__)

Hope this makes sense.

Stéphane


On Sat, Mar 21, 2020 at 7:08 PM Hailiang Shen <hailiang@...> wrote:

Hello all, could anyone help me understand printing float values? E.g.

 

print (7.0/3) will output 2.33333333333

print (7/3.0, 7.0/3) will output (2.3333333333333335, 2.3333333333333335)

 

I am not sure why the outputs are slightly different.

 

Regards,

 

Hailiang

 

Join users@ironpython.groups.io to automatically receive all group messages.