
Nick Coghlan wrote:
Kristján Valur Jónsson wrote:
So, maybe I should rephrase the "idea." The idea is for example to speed up the initialization of lists (and subclasses of lists) from other lists when the source list's contents is to be discarded. The semantics of a.swap(b) are the same as a[:], b[:] = b[:], a[:] but it a) is orders of magnitude faster b) skips any special methods (such as __setslice__) and so may break class invariants of list subclasses, as pointed out on this list.
I'm not sure it is actually practical to make munging about with list internals like this "safe" enough to be comfortably exposed at the Python level, but ideas like the above would at least be a step in that direction.
It sounds to me like what is really desired in this case is not a copy/swap operation but a type mutate operation on the data. So instead of copying the data, the data's type is mutated to another type providing that the data format is compatible. So the above becomes something like... a_type = type(a) b_type = type(b) a.mutate_type_to(b_type) b.mutate_type_to(a_type) But the more common use would be just to convert a single data object to another similar data object in a much more efficient way. Would that even be possible? A class would need to know what other classes it is data compatible with somehow. Possibly that could be done by registering that before use. Then it would need the machinery to do that, which I have no idea about. The advantage is it's a more general way to approach the problem that could save a lot of data copying in more situations. Ok, it's a wild idea of sorts, but this is the idea list. ;-) Cheers, Ron