I have a class ( dict
s list) and I want it to sort itself:
class Table(list): … def sort (self, in_col_name): self = Table(sorted(self, key=lambda x: x[in_col_name]))
but it doesn’t work at all. What for? How to avoid this? With the exception of external sorting, for example:
new_table = Table(sorted(old_table, key=lambda x: x['col_name'])
Is it impossible to manipulate the object itself? It is more important to have:
class Table(list): pass
than:
class Table(object): l = [] … def sort (self, in_col_name): self.l = sorted(self.l, key=lambda x: x[in_col_name])
which, I think, works. And in general, is there no way in Python that an object can change itself (not just an instance variable)?
source share