3 ms·
I'm guessing, but a function that can copy any object is unlikely to be as efficient as one that works with known types. Of course, even if true, that may not
by JabavuAdams 18y ago
I'm guessing, but a function that can copy any object is unlikely to be as efficient as one that works with known types.
Of course, even if true, that may not matter.
See, I'm all about the weasel words today.
- cstejerean 18y agoIt would be a little faster. The first part of copy.copy looks like def copy(x): """Shallow copy operation on arbitrary Python objects. See the module's __doc__ string for more info. """ cls = type(x) copier = _copy_dispatch.get(cls) if copier: return copier(x) # more after this point but it's not relevant for lists So calling copy.copy on a list over using list() will check for the type, look up the type in a dictionary after which it will proceed as if you called list() yourself.
- JabavuAdams 18y agoMental memo. :)
- palish 18y agoI'm not sure either... A hand-written .clone() method would always beat the generic solution of course, but it seems like the tradeoff is that you spend less time coding, which is a win.