[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: is != == (Re: [pygame] Sticky Variables: Terrain Loading)



Michael George wrote:
Heh, so much for "there's only one way to do it in python".  We have:

y = [x[1], x[2], ..., x[n]]
y = list(x)
y = x[:]
y = deepcopy(x) # slightly different
import copy
y = copy.copy(x)

does the same thing as the others (shallow copy)

and I would have said

y = [z for z in x]

I think of these, y = list(x) is probably the "right" way.
There are other ways too... you could map an anonymous lambda that does nothing to the list, or filter it and let every item pass, or.... "there's one way" means that there's usually one straightforward, easy way and many other ways that you should avoid unless necessary. For example, if you wanted to perform some kind of transform on the list, then some of these methods wouldn't be the "right way" because they wouldn't be able to accommodate that. The first one is the only one that I don't like. It's assuming the size of x is at least a certain size, and it's hardcoding the length of y. Perhaps this is desired in some cases but usually I'd say this is bad.