len
used as condition PYL-C1802len(SEQUENCE)
without comparison to determine if a sequence is empty 916 pagination_selections = self.get_selections(info, ['pagination']) or []
917 pagination_names = list(map(lambda x: x.name.value, pagination_selections))
918
919 if len(pagination_names): 920 limit = self.get_pagination_limit(pagination)
921 offset = pagination.get('offset')
922 page = pagination.get('page')
len(SEQUENCE)
without comparison to determine if a sequence is empty 871 data_names = list(map(lambda x: x.name.value, data_selections))
872 model_attrs = dict(map(lambda x: [clean_name(x), getattr(Model, x)], dir(Model)))
873 only_columns = list(filter(lambda x: x is not None, map(lambda x: model_attrs.get(x), field_names))) \
874 if len(field_names) and 'allAttrs' not in data_names else None 875
876 queryset = self.get_queryset(request, Model, only_columns)
877
len(SEQUENCE)
without comparison to determine if a sequence is empty 596 lambda x: getattr(x['instance'], lookup_data['source_column'], None) in values,
597 lookup_data['aggregated_values']
598 ))
599 item_result['aggregated'] = model_values[0]['value'] if len(model_values) else 0 600
601 result[lookup_name] = item_result
602
len(SEQUENCE)
without comparison to determine if a sequence is empty 583 if lookup_data['return_list']:
584 item_result['value'] = values
585 else:
586 item_result['value'] = values[0] if len(values) else None 587
588 if 'related' in lookup_data:
589 item_result['related'] = self.filter_lookup_models(
len(SEQUENCE)
without comparison to determine if a sequence is empty 399 )
400 break
401 else:
402 if len(parent_relations): 403 last_related_model = None
404
405 for relationship in parent_relations:
Using the len
function to check if a sequence is empty is not idiomatic and can be less performant than checking the truthiness of the object.
len
doesn't know the context in which it is called, so if computing the length means traversing the entire sequence, it must; it doesn't know that the result is just being compared to 0.
Computing the boolean value can stop after it sees the first element, regardless of how long the sequence actually is.
Not preferred:
if not len(my_sequence):
print("Empty sequence.")
else:
print("Sequence is not empty.")
Preferred:
if not my_sequence:
print("Empty sequence.")
else:
print("Sequence is not empty.")