PEP 526 - var annotations and the spirit of python

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Jul 5 06:04:13 EDT 2018


On Thu, 05 Jul 2018 09:17:20 +0200, Christian Gollwitzer wrote:

> Am 04.07.18 um 17:31 schrieb Steven D'Aprano:
>> On Wed, 04 Jul 2018 13:48:26 +0100, Bart wrote:
>> 
>>> Presumably one type hint applies for the whole scope of the variable,
>>> not just the one assignment.
>> 
>> You know how in C you can write
>> 
>>      int x = 1;  # the type applies for just this one assignment x =
>>      2.5;    # perfectly legal, right?
>> 
>> 
> Not sure what point you are trying to make, but your example compiles in
> C, if you replace the '#' comment sign with '//'.


Oops.


But... it compiles? Seriously? 

> Only it doesn't do
> what you might think: the 2.5 is down-converted to an integer, therefore
> x will be 2 in the end. There will be a compiler warning but no error.

Sometimes I wonder how C programmers manage to write a bug-free "Hello 
World" program. No wonder it is described as a type-unsafe language or a 
weakly-typed language.

I understand upcasting ints to floats, that's cool (even if a few 
languages take a hard line on that too, I don't). I understand Python's 
dynamic typing approach. I don't understand C requiring type 
declarations, then down-casting floats to integers.

At least it shows a warning. But hell, who pays attention to C compiler 
warnings? There's *so many of them*.

(And I bet that by default the warning is disabled, amirite?)



-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson



More information about the Python-list mailing list