It seems the topic of this thread has changed drastically from the original
message.
1) "while EXPR as VAR" in no way says that EXPR must be a boolean value. In
fact, a use case I've run into commonly in web development is popping from a
redis set. E.g.
client = StrictRedis()
while True:
profile_id = client.spop("profile_ids")
if not profile_id:
break
print profile_id
In this case, profile_id is "None" when the loop breaks. It would be much more
straightforward (and more Pythonic, IMO), to write:
client = StrictRedis()
while client.spop("profile_ids") as profile_id:
print profile_id
2) Although not originally intended, I kind of like the "if" statement change
proposed later in this thread. It certainly makes sense, since both while and
if are "conditional" statements that are commonly followed by an assignment (or
vice versa).
3) I don't think the use case I brought up is solved nicely by wrapping a
function / lambda in a generator and using a for loop. E.g.
def helper(f):
value = f()
if value:
yield value
for profile_id in helper(lambda: client.spop("profile_ids")):
print profile_id
This works too, I guess
def helper(f, *args, **kwargs):
value = f(*args, **kwargs)
if value:
yield value
for profile_id in helper(client.spop, "profile_ids"):
print profile_id
Either way, it adds too much mental overhead. Every developer on a project has
to now insert x lines of code before a for loop or import a helper method from
some module, and do this every time this pattern reappears. It's not something
I would want to do in one of my projects, since it makes things harder to
understand. So all in all, it's a net negative from just doing things the
canonical way (with the while / assignment pattern).
Dan
--
http://mail.python.org/mailman/listinfo/python-list