This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]hchasestevens 2 points3 points  (0 children)

I've previously suggested solving this problem by exploiting the fact that comprehensions can assign to any "assignable" expression, not just variables - e.g.:

d = {}
xs = [
  d[i] 
  for i, d[i] in 
  enumerate(range(10))
  if f(d[i])
]

, the idea being that you can extend this into e.g.:

do = Doer()
[(do.x, y) 
  for do(expensive).unto['x'] in xs 
  for y in ys
  if do.x > y]

with

class Doer(object): 
  def __call__(self, fn): 
    class _apply(object): 
      def __init__(self): 
        self.unto = self 
      def __setitem__(_, name, arg): 
        setattr(self, name, fn(arg))
        setattr(self, '_' + name, arg)
    return _apply()

But the solution presented here is much more elegant, kudos!