使用Python的理由:
- Variable is typeless, it's a reference. Value has type!
- Two kinds of index out of range,positive or negative.
- 海象操作符
:=
(walrus operator)优先级比较低,请一定括起来。正如一定要将位运算括起来一样。- 用空格缩进表示code block,空格数
>=1
即可,但同一个code block的每行空格数要对齐。- 数字(各种进制)中的下划线
_
是为了方便阅读,不影响数字的值。比如:12_34.56_78
,0xAA_BB_CC_DD
或0b0101_1010
。- Python的赋值语句没有返回值,连续赋值或用unpack tuple赋值,执行逻辑都是
从左到右
(与C语言相反),要特别小心:
>>> a = [1,2,3,4,5,6,7]
>>> i, j = 1, 2
>>> i = a[i+j] = j = 3
>>> print(i,j,a)
3 3 [1, 2, 3, 4, 5, 3, 7]
>>> i, i = 1, 2 # i is 2
>>> obj.a, obj.a.p = x, y # obj.a has been changed first!!
- Python的For循环结束,循环变量停在最后一个有效值上:
>>> for i in range(10): pass
...
>>> i
9
- The execution of a function introduces a new symbol table used for the local variables of the function. More precisely, all variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the local symbol tables of enclosing functions, then in the global symbol table, and finally in the table of built-in names. Thus, global variables and variables of enclosing functions cannot be directly assigned a value within a function (unless, for global variables, named in a
global
statement, or, for variables of enclosing functions, named in anonlocal
statement), although they may be referenced.- The actual parameters (arguments) to a function call are introduced in the
local symbol table
of the called function when it is called; thus, arguments are passed usingcall by value
(where the value is always an object reference, not the value of the object). When a function calls another function, or calls itself recursively, a new local symbol table is created for that call.- Even for a function without return statement, it actually
return None
.- Local variables of a function can be returned in Python. They are not on the stack, they are on heap. Actually, only reference is returned to use outside function.
-- 目录[1] --
-- 文章[60] --