Ethan Furman added the comment:
Guido van Rossum opined:
I still think the problem is with your class design.
You shouldn't want a hex representation for a value
that's not an integer.
Well, in fairness I only supported it because bool does, and I was trying to
have
New submission from Ethan Furman:
In Py3k __hex__ and __oct__ were removed, and hex() and oct() switched to use
__index__.
hex() and oct() should be using __int__ instead.
Having read through PEP 357 [1] I see that __index__ is /primarily/ concerned
with allowing arbitrary objects to be used
Serhiy Storchaka added the comment:
__index__() is used because float has __int__ but not __index__.
(42.0).__int__()
42
(42.0).__index__()
Traceback (most recent call last):
File stdin, line 1, in module
AttributeError: 'float' object has no attribute '__index__'
--
nosy:
Antoine Pitrou added the comment:
Indeed, the definition and use of __index__ has derived since PEP 357.
Nowadays, __index__ means can be converted to an int without loss.
In any case, I find the behaviour of your logical type a bit dubious. If it's
like bool but ternary, it *should* convert
Mark Dickinson added the comment:
hex() and oct() should be using __int__ instead.
Strong -1 from me. I wouldn't want `hex(45.3)` to work, and `hex(45.0)`
working isn't much better.
--
___
Python tracker rep...@bugs.python.org
Stefan Krah added the comment:
-1 from me as well. I would not want to audit a large program for
accidentally converted floats.
--
nosy: +skrah
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue19988
Raymond Hettinger added the comment:
Guido, I believe that __index__ was your initiative. Do you care to opine on
this one?
--
assignee: - gvanrossum
nosy: +gvanrossum, rhettinger
___
Python tracker rep...@bugs.python.org
Guido van Rossum added the comment:
I agree with Mark and Stafan. Hex/oct/bin are only defined for integers.
__int__ is ambiguous -- it has the same problem as (int) in C in that it
applies to floats and then loses the fraction.
I think the problem with Ethan's ternary logic is that it
Ethan Furman added the comment:
For the record, the true/false values of my Logical type do convert to int,
just not the unknown value.
I agree using __int__ is dubious because of float (and Decimal, etc.), which
means really the only clean way to solve the issue (definitely for me, and for
Guido van Rossum added the comment:
I still think the problem is with your class design. You shouldn't want a hex
representation for a value that's not an integer.
For the difference between %x and hex() please open another issue (you might
want to track down the cause in the source first so
10 matches
Mail list logo