[Python-ideas] isinstance(Decimal(), Real) -> False?

Steven D'Aprano steve at pearwood.info
Thu Aug 29 04:04:07 CEST 2013


On 28/08/13 22:06, Vernon D. Cole wrote:

> Now, consider when I am processing the arguments for an SQL "execute"
> method. [*]  How do I prepare the values for the underlying db engine? I
> use a long list which includes lots of "elif isinstance(value, <some Python
> type>):"
>
> The code for "isinstance(value, Real)" is quite straight forward.

number.Real is not merely a synonym for built-in float.

For example, should somebody decide to implement a floating point number class based on base 3 rather than 2 or 10 (perhaps they are trying to emulate some old Soviet ternary-based computer), it would be a subclass of Real.


>The code for "isinstance(value, decimal.Decimal)" requires 18 lines of
> incredibly obscure Python.
>
> I really do need to be able to tell them apart.

You can always tell them apart. Decimal instances are instances of Decimal. Non-Decimal instances are not.



-- 
Steven


More information about the Python-ideas mailing list