

def sort_by_popularity(items):
    return sorted(items, key=lambda product: product.popularity, reverse=True)


def sort_by_property(prop):
    def _sort_by_property(items):
        return sorted(items,
                      key=lambda item: getattr(item, prop),
                      reverse=True)

    return _sort_by_property

sort_by_popularity = sort_by_property('popularity')

Redefine sort function to a generic one. Some overhead but can use for any property