python - Why the following is allowed in C? -
why following behavior allowed implementations of c ? if use variable define it, this:
#include <stdio.h> int main() { int = 9; int b = ( +b ); printf("%d",b); }
i know garbage value of b
used there should compile time warnings . sounds little weird me. in languages python illegal such stuff :
>>> b = 9 >>> = + b traceback (most recent call last): file "<pyshell#2>", line 1, in <module> = + b nameerror: name 'a' not defined
i guess python not put variable current namespace until well-defined (i.e. has value within current scope) while in c above statement breaks this:
int b = 9; int a; // put namespace or whatever lookup table garbage value = + b;
it nice if points out philosophy behind this.
explanation
in c function, of local variable declarations happen straight away. essentially, compiler translates code into:
int a; int b; // start of function = 9; b = + b; // end of function
so can see, there variable b
, exists whole runtime of function.
in python, doesn't happen. b
not exist until assigned first time.
the python interpreter runs code a+b
before runs b = <the result of a+b>
. if think it's doing intermediate result equivalent to:
a = 9; intermediate_value = + b b = intermediate_value
it clear using b
before defined.
philosophy
this comes down difference between static language c, , dynamic language python. in dynamic language, lot of things done compile time in static language done @ runtime instead. such things can include modifying code of running program itself. (in theory, unfettered access computer's memory, in c too; on modern operating systems, impossible).
have read of page http://en.wikipedia.org/wiki/dynamic_programming_language more information.
Comments
Post a Comment