PEP 526 - var annotations and the spirit of python

Christian Gollwitzer auriocus at gmx.de
Thu Jul 5 03:17:20 EDT 2018


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 '//'. 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.


=====================================
Apfelkiste:Tests chris$ cat intx.c
#include <stdio.h>
int main() {
	int x=1;
	x=2.5;
	printf("%d\n", x);
	return 0;
}

Apfelkiste:Tests chris$ gcc intx.c -o intx && ./intx
intx.c:4:4: warning: implicit conversion from 'double' to 'int' changes 
value
       from 2.5 to 2 [-Wliteral-conversion]
         x=2.5;
          ~^~~
1 warning generated.
2



	Christian


More information about the Python-list mailing list