Jesse's Blog » 日志 » Notes on <Learning Python>
Notes on <Learning Python>
wanderer 发表于 2008-05-20 18:33:50
Part II. Types and Operations
1.Manual type testing is usually not the right thing to do in Python. (P.124)
2.Don't use cyclic reference, unless you need to. (P.127)
3.Slices are always extracted from left to right.
L[3:1] equals L[3:3], and L[3:1] = ['foo'] is inserted between L[2] and L[3]
But L[3:1:-1] is extracted from right to left.(==> L[3],L[2],L[1])
4.Variable names work like dictionary keys; they must have already been assigned when referenced, but are created when first assigned.
5.'+' doesn't work for dictionaries, because they are not sequences.
6.Remember: indexing a string always generates a string. S[0] ==> a string
Part III .Statements and Syntax
7.L += [4] is different to L = L + [4], not only speed.
>>> L = [1,2]
>>> X = [L,3]; X
[[1, 2], 3]
>>> L += [4]; L
[1, 2, 4]
>>> X
[[1, 2, 4], 3]
############
>>> x = [1,2]
>>> y = [x,3]; y
[[1, 2], 3]
>>> x = x + [4]; x
[1, 2, 4]
>>> y
[[1, 2], 3]
8.In Python, statements can't be used as expressions. e.g., we can't embed assignment statements in other expressions
1.Manual type testing is usually not the right thing to do in Python. (P.124)
2.Don't use cyclic reference, unless you need to. (P.127)
3.Slices are always extracted from left to right.
L[3:1] equals L[3:3], and L[3:1] = ['foo'] is inserted between L[2] and L[3]
But L[3:1:-1] is extracted from right to left.(==> L[3],L[2],L[1])
4.Variable names work like dictionary keys; they must have already been assigned when referenced, but are created when first assigned.
5.'+' doesn't work for dictionaries, because they are not sequences.
6.Remember: indexing a string always generates a string. S[0] ==> a string
Part III .Statements and Syntax
7.L += [4] is different to L = L + [4], not only speed.
>>> L = [1,2]
>>> X = [L,3]; X
[[1, 2], 3]
>>> L += [4]; L
[1, 2, 4]
>>> X
[[1, 2, 4], 3]
############
>>> x = [1,2]
>>> y = [x,3]; y
[[1, 2], 3]
>>> x = x + [4]; x
[1, 2, 4]
>>> y
[[1, 2], 3]
8.In Python, statements can't be used as expressions. e.g., we can't embed assignment statements in other expressions
收藏:
QQ书签
del.icio.us
订阅:
Google
抓虾
