def frepr(var, ffmt="%.16e"):
"""Similar to Python's repr(), but return floats formated with `ffmt` if
`var` is a float.
If `var` is a string, e.g. 'lala', it returns 'lala' not "'lala'" as
Python's repr() does.
Parameters
----------
var : almost anything (str, None, int, float)
ffmt : format specifier for float values
Examples
--------
>>> frepr(1)
'1'
>>> frepr(1.0)
'1.000000000000000e+00'
>>> frepr(None)
'None'
>>> # Python's repr() does: 'abc' -> "'abc'"
>>> frepr('abc')
'abc'
"""
if isinstance(var, types.FloatType):
return ffmt %var
elif isinstance(var, types.StringType):
return var
else:
return repr(var)
评论列表
文章目录