I have a case in which I need to determine whether a for loop yielded at least one element:
@property
def source_customers(self):
"""Yields source customers"""
for config in RealEstatesCustomization.select().where(
RealEstatesCustomization.customer == self.cid):
yield config.source_customer
# Yield self.customer iff database
# query returned no results
try:
config
except UnboundLocalError:
yield self.customer
Is this a pythonic way to go or shall I rather use a flag for that like so:
@property
def source_customers(self):
"""Yields source customers"""
yielded = False
for config in RealEstatesCustomization.select().where(
RealEstatesCustomization.customer == self.cid):
yield config.source_customer
yielded = True
# Yield self.customer iff database
# query returned no results
if not yielded:
yield self.customer
What is preferable?
source_customers
returnedself.customer
? Isn't that weird? – Barry Dec 7 '15 at 14:56peewee
based ORMRealEstatesCustomization
. If no deviating customer(s) is specified within the database configuration, the real-estate customer is assumed to be the instance's customer. – Richard Neumann Dec 7 '15 at 16:07