>>> t = (1,2,[30,40]) >>> t[2] += [50, 60] 会发生什么, 选择最佳答案: a) t变成(1, 2, [30, 40, 50, 60]) b 抛出TypeError, 'tuple'object does not support item assignment异常,即元祖对象不支持赋值 c) 上面两种情况都没有发生. d) a 和 b都发生
An object is hashable if it has a hash value which never changes during its lifetime (it needs a hash() method), and can be compared to other objects (it needs an eq() method). Hashable objects which compare equal must have the same hash value.
>>> import registration running register(<function f1 at 0x10063b1e0>) running register(<function f2 at 0x10063b268>) >>> registration.registry [<function f1 at 0x10063b1e0>, <function f2 at 0x10063b268>]
编写一个装饰器
下面编写一个记录函数执行时间的装饰器,保存到clockdeco.py中
1 2 3 4 5 6 7 8 9 10 11
import time def clock(func): def clocked(*args): # t0 = time.perf_counter() result = func(*args) # elapsed = time.perf_counter() - t0 name = func.__name__ arg_str = ', '.join(repr(arg) for arg in args) print('[%0.8fs] %s(%s) -> %r' % (elapsed, name, arg_str, result)) returnresult return clocked
之后使用这个装饰器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import time from clockdeco import clock @clock defsnooze(seconds): time.sleep(seconds)
@clock deffactorial(n): return1if n < 2else n*factorial(n-1)