On Sat, Oct 02, 2021 at 01:06:46PM -0000, Debashish Palit wrote:

[...]
> Consider a number radix conversion software. It needs to accept the 
> input number as a string (say '15A' in base 12). When we provide it a 
> base 10 number, it would also be a string.

Why a string? That's an unusual API. The more usual API would be:

    str (any base) --> int or float
    int or float --> str (any base)

Not that it matters. Your "isfloat()" function doesn't help you.

    # Base 12 number, as a string.
    "2ab9.6".isfloat()
    # returns None for not a float or an int

(That's 4461.5 in decimal.) So you can't use your isfloat method in this 
case, and you don't need to. All you need to do is check whether there 
is a radix point "." in the string.


> > If you need to preserve the exact input for the repr method, you should 
> > preserve the actual input.
> > class MyClass:
> >         def __init__(self, astring):
> >             self._arg = astring
> >             self.number = float(astring)
> >         def __repr__(self):
> >             return 'MyClass(%s)' % self._arg
> 
> Here you are storing the same info twice - as a string and as a 
> number. Why not just store a number?

Because just storing the number loses information. If you need the 
*exact* input then just storing the results after conversion to float 
or int has lost detail:

    int("  123_456  ")
    # returns 123456

    float("  +2.5000000000  ")
    # returns 2.5

    float("7.3e5")
    # returns 730000.0

You can lose whitespace, underscores, leading plus sign, trailing 
zeroes, exponential format, and more. There is no way to recreate the 
exact input from the number.

I don't know why you want to show the exact input. It is hard for me to 
see why it would be important. But **if** you do, and the input is a 
string, then keeping that string is the only way to preserve that exact 
input without losing detail.


-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ECYRRCV27FBFXZCIAQF62DNCHATTIL3S/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to