pypy - Why doesn't Python always require spaces around keywords? -
why can spaces omitted before , after key words? example, why expression 2if-1e1else 1
valid?
seems work in both cpython 2.7 , 3.3:
$ python2 python 2.7.3 (default, nov 12 2012, 09:50:25) [gcc 4.2.1 compatible apple clang 4.1 ((tags/apple/clang-421.11.66))] on darwin type "help", "copyright", "credits" or "license" more information. >>> 2if-1e1else 1 2 $ python3 python 3.3.0 (default, nov 12 2012, 10:01:55) [gcc 4.2.1 compatible apple clang 4.1 ((tags/apple/clang-421.11.66))] on darwin type "help", "copyright", "credits" or "license" more information. >>> 2if-1e1else 1 2
and in pypy:
$ pypy python 2.7.2 (341e1e3821ff, jun 07 2012, 15:42:54) [pypy 1.9.0 gcc 4.2.1] on darwin type "help", "copyright", "credits" or "license" more information. , different: ``pypy 1.6 released!'' >>>> 2if-1e1else 1 2
identifiers in python described as:
identifier ::= (letter|"_") (letter | digit | "_")*
hence, 2if
can't identifier hence if must 2
,if
. similar logic applies rest of expression.
basically interpreting 2if-1e1else 1
go (the full parsing quite complicated):
2if
not valid identifier, 2
matches digit digit ::= "0"..."9"
,if
matches keyword. -1e1else
, -1
unary negation (u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr
) of : ( 1
matches intpart
in exponentfloat ::= (intpart | pointfloat) | exponent
, e1
exponent exponent ::= ("e" | "e") ["+" | "-"] digit+
.) can see expressions of form ne+|-x
yields float from:
>>> type(2e3) <type 'float'>
then else
seen keyword, , -1
etc..
you can peruse gammar read more it.
Comments
Post a Comment