Code review comment for lp://staging/~barry/autopilot/lp1488175

Revision history for this message
Max Brustkern (nuclearbob) wrote :

> On Aug 27, 2015, at 08:22 PM, Max Brustkern wrote:
>
> >> @@ -39,8 +40,7 @@
> >> """Must return a backend when called with a single backend."""
> >> class Backend(object):
> >> pass
> >> - _create_backend = lambda: Backend()
> >> - backend = _pick_backend(dict(foo=_create_backend), '')
> >> + backend = _pick_backend(dict(foo=Backend), '')
> >
> >I'm not quite a clever in python as everyone else here, so I just want to
> >make sure that Backend is wanted here and not Backend() This seems to apply
> >to most of the tests around this, so I imagine they'd be breaking if it were
> >incorrect, I just like to be sure it's intentional.
>
> It is!
>
> lambdas are defined here:
>
> https://docs.python.org/3/reference/expressions.html#lambda
>
> The example given provides a great way to think about them.
>
> lambda args: expression
>
> is equivalent to
>
> def <lambda>(args):
> return expression
>
> where the function is anonymous, i.e. it performs no name binding in the
> current namespace. When you assign the `lambda: Backend()` to
> _create_backend, it's the assignment that does the name binding to the
> function object, so its exactly equivalent to:
>
> def _create_backend():
> return Backend()
>
> Note too that when _pick_backend() is called, the code is passing in a
> dictionary that maps the `foo` key to the *function object* created by the
> lambda; specifically, the calling of the lambda-created function object is
> deferred. It's exactly equivalent to just setting `foo` to the Backend
> function object without calling it.

Yeah, that's what was confusing me. I saw:
_create_backend = lambda: Backend()
and assumed that would return Backend() (the return value of that, i.e. an instance of the class) and not Backend (the class itself) The rest of the lambda syntax made sense to me, I just tripped over some parentheses here, since we're setting foo=Backend and not foo=Backend() like I would have expected from the lambda. Exciting stuff!

« Back to merge proposal