Easier, Faster Property Enumeration in Python
I just discovered a really neat trick in Python to take a collection of objects, and turn one of their properties into a list. It's not terribly difficult to perform this the old way...
subscribers = []This would return something like [(User:bob),(User:jerry),(User:tim)] and so on. However, this can be done in just a single line...
for subscription in self.subscriptions.all():
subscribers.append(subscription.user)
return subscribers
return [s.user for s in self.subscriptions.all()]+1 for Python coolness.
0 comments:
Post a Comment