Issue 6044: Exception message in int() when trying to convert a complex (original) (raw)

Created on 2009-05-17 03:18 by aletornw, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg87955 - (view) Author: (aletornw) Date: 2009-05-17 03:18
Hi, I noticed that when trying to convert with the int() function a complex number the exception error says "use int(abs(z))". I think that insted it should say "use int(z.real)" because if I want to convert 1j**2 to int using the abs method, the result is 1 positive, when it should be -1. With my method it works fine.
msg87974 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-05-17 09:21
That no unambiguous conversion between complex and int is defined is exactly the reason for this error message. You could want the absolute value, the real part, the imaginary part, or even the polar angle... int(abs(z)) works as intended, giving you the absolute value of the complex number, which is 1 for 1j**2.
msg87976 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-05-17 09:49
I always found the "use int(abs(z))" part of that message odd, as well. As Georg points out, there are many possible ways that one might want to convert complex to int; it seems strange to give advice for one particular one when that may well not match what the user wanted. I'd suggest just dropping the "use int(abs(z))" from the error message. I think it's more likely to be confusing than helpful. The same applies to float(complex).
msg87984 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-05-17 10:49
Removed "; use int(abs(z))" from the error message (and the corresponding pieces from the error messages for long(z) and float(z)) in r72718, r72719, r72720, r72722. (Georg agreed to this change in a brief discussion on IRC.)
History
Date User Action Args
2022-04-11 14:56:49 admin set github: 50294
2009-05-17 10:49:25 mark.dickinson set resolution: not a bug -> fixedmessages: +
2009-05-17 09:49:29 mark.dickinson set nosy: + mark.dickinsonmessages: +
2009-05-17 09:21:05 georg.brandl set status: open -> closednosy: + georg.brandlmessages: + resolution: not a bug
2009-05-17 03🔞00 aletornw create