Повторное использовние, декораторы, оператор with

Сборочное программирование и повторное использование

Декораторы

Оператор with. ContextManager

Д/З

(необязательное)

def f1(n):
  print 'f1 called'
  return n*n

@mod.function_cachier
def f2(n):
  print 'f2 called'
  return n*n*n

print len([ f1(n) for n in range(3)])
print len([ f1(n) for n in range(3)])
print len([ f2(n) for n in range(4)])
print len([ f2(n) for n in range(4)])

f1 called
f1 called
f1 called
3
f1 called
f1 called
f1 called
3
f2 called
f2 called
f2 called
f2 called
4
4

def f1(n):
  print 'f1 called'
  return n*n

def f2(n):
  print 'f2 called'
  return n*n*n

print len([ f1(n) for n in range(3)])

with mod.with_function_cachier(f2) as f:
  print len([ f(n) for n in range(5)])

f1 called
f1 called
f1 called
3
5

LecturesCMC/PythonIntro2014/10_Decorators_With (последним исправлял пользователь FrBrGeorge 2015-07-02 18:27:03)