I have a list of objects in Python 3.5.1 with several named attributes. The list represents a group of C variables and as such each object has three named attributes that correspond to three common base types: uint8, uint16, and uint32.
class variableList(object):
def __init__(self, eight, sixteen, thirtytwo):
self.size_8 = eight
self.size_16 = sixteen
self.size_32 = thirtytwo
The value of these named attributes is either (Nonetype) None or (str)'E'. Per object, only one of the named attributes will have some non-None value.
I want to sort this list of objects such that all objects with non-None value for size_32 are on top, followed by size_16, followed by size_8.
In python 2.7.1 I successfully used this, but it felt like black magic:
size_filtered_list = sorted(filtered_list, key=lambda y: (y.size_32, y.size_16, y.size_8))
but in 3.5.1 this no longer works. Suggestions?
__lt__and__eq__methods you could sort your variables without akeyargument. Because I am unfarmiliar with C could you explain basically what(str)'E'would mean in python?Noneand'E'rather than just usingFalseandTrue? If you weren't using incompatible types, your sort could simplify to an import at the top of the file,from operator import attrgetter, with the sort:sorted(filtered_list, key=attrgetter('size_32', 'size_16', 'size_8')), which works fine in all versions of Python because you're sorting comparable types.self.size_8_exist, you can still use the same technique.