I\'m trying to sort a list of objects using
my_list.sort(key=operator.attrgetter(attr_name))
but if any of the list items has attr = None
Since there are other things besides None that are not comparable to a string (ints and lists, for starters), here is a more robust solution to the general problem:
my_list.sort(key=lambda x: x if isinstance(x, str) else "")
This will let strings and any type derived from str to compare as themselves, and bin everything else with the empty string. Or substitute a different default default key if you prefer, e.g. "ZZZZ" or chr(sys.maxunicode) to make such elements sort at the end.