正文
return
when
...
>>>
report
()
1500113234.487932
>>>
report
()
1500113234.487932
python docoment 给出了标准的解决办法:
A way around this is to use None as the default, and explicitly test for it in the body of the function
>>>
def
report
(
when
=
None
)
:
...
if
when
is
None
:
...
when
=
time
.
time
()
...
return
when
...
>>>
report
()
1500113446.746997
>>>
report
()
1500113448.552873
第二: x += y vs x = x + y
一般来说,二者是等价的,至少看起来是等价的(这也是陷阱的定义 — 看起来都OK,但不一定正确)。
>>>
x
=
1
;
x
+=
1
;
print
x
2
>>>
x
=
1
;
x
=
x
+
1
;
print
x
2
>>>
x
=
[
1
];
x
+=
[
2
];
print
x
[
1
,
2
]
>>>
x
=
[
1
];
x
=
x
+
[
2
];
print
x
[
1
,
2
]
呃,被光速打脸了?
>>>
x
=
[
1
];
print
id
(
x
);
x
=
x
+
[
2
];
print
id
(
x
)
4357132800
4357132728
>>>
x
=
[
1
];
print
id
(
x
);
x
+=
[
2
];
print
id
(
x
)
4357132800
4357132800
前者x指向一个新的对象,后者x在原来的对象是修改,当然,那种效果是正确的取决于应用场景。至少,得知道,二者有时候并不一样
第三,神奇的小括号–()
小括号(parenthese)在各种编程语言中都有广泛的应用,python中,小括号还能表示元组(tuple)这一数据类型, 元组是immutable的序列。
>>>
a
=