JEP 495 Question

Johannes Spangenberg johannes.spangenberg at hotmail.de
Sat Nov 9 17:32:17 UTC 2024


>>> Python: loan = input("Loan: ")
>> Correct input would be: loan = float(input(...))
> I tested the code that line was from using PyCharm as my IDE and it 
> ran without any errors or warnings.

Have you also verified the result. If you have used "+" or "*" as the 
operator, then your script might have run successfully, but it might not 
have done what you have expected.

    Python 3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> loan = input("Loan: ")
    Loan: 5
    >>> loan + loan
    '55'

Python has a rather strict (but dynamic) type system. There is no type 
coercion. If you would use another operator, like "/", you would get an 
exception. You would also get an exception with "+" if you mix the types 
(e.g. 100 + "5").

> Dont confuse good languages with js :)

In JS, you might actually also have to parse the string, unless you have 
used an operator which is not defined on strings, in which case type 
coercion might do the conversion for you. But relying on type coercion 
is also often seen as bad practice and nothing I would see as an 
advantage. For example "5" + "5" is also "55" in JavaScript.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20241109/ef531fe4/attachment.htm>


More information about the amber-dev mailing list