lambda - lazy evaluation and late binding of python? -


when lazy evaluation? (generator, if, iterator?), when late binding? (closure, regular functions?)

    = [1,2,3,4]     b = [lambda y: x x in a]      c = (lambda y: x x in a) #lazy evaluation     d = map(lambda m: lambda y:m, a) #closure     in b:         print i(none)     # 4 4 4 4     in c:         print i(none)     # 1 2 3 4      in d:         print i(none)     # 1 2 3 4 

this looks homework, won't give answers. here 2 functions can step through , see how values change.

def make_constants_like_generator():     def make_constant(x):         def inner(y):             return x         return inner     results = []     in [1, 2, 3, 4]:         results.append(make_constant(i))         f in results:             print f(none)     return results  def make_constants_like_list():     x = none     results = []     in [1, 2, 3, 4]:         x =         def inner(y)             return x         results.append(inner)         f in results:             print f(none)     return results 

lazy evaluation waiting until last possible moment evaluate expression. opposite eager evaluation. generator expression lazy, nothing until iterated. list expression eager, encountered, list filled values.

early , late binding relate how system determines name refers to. names in python late bound. combined lazy evaluation means name bound can change before expressions use evaluated

def map(func, iter):     return (func(val) val in iter) 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -