3 ms·
Huh, I've almost never seen [:], would never think of it. Do people really use that? I mean it takes a lot of work to make python cryptic but I guess if you'r
by njharman 18y ago
Huh, I've almost never seen [:], would never think of it. Do people really use that? I mean it takes a lot of work to make python cryptic but I guess if you're determined anything is possible.
use deepcopy or list().
I don't even use [] or {} to create empties anymore. I much prefer explicit esp since there is proliferation of container types and it's silly, inconsistant, and confusing that dicts and lists have syntactic exceptions.
newlist = list()
newdict = dict()
newdict = defaultdict(str)
etc.
And to anuraggoel. not using [:] is not avoiding slices. just as not using
string += "ext"
(esp in loop) is not avoiding strings.
- nostrademons 18y agoI always used [:], although now I think I'll switch to copy.copy or list() after reading this thread. (Actually, now that I work for BigCo, I'll have to check what their coding standards say about this.)
- thorax 18y agoI've also used list() because I find it just generally more readable.
- ivank 18y agoCreating empties with {} takes just 58% of the time as dict() [0.25 us, 0.39 us] because there's no need to LOAD_GLOBAL and CALL_FUNCTION. 'not not variable' is similarly faster than bool(variable). (not that I've run into this often in Python.)
- njharman 18y ago> takes just 58% of the time Whoopee fucking doo, really.