is cryptic? I'd say it follows directly from how slice notation works. If you omit the first index, the slice goes to the beginning of the list. If you omit the second index, the slice goes to the end of the list. So, if you omit both indices, everything works as you'd expect it to if you understand slice notation. Anyone working with lists in Python should understand slice notation, so I fail to see the problem here.
That line means "list1 is a list that has all the elements of list2", not "list1 is a new list that has all the elements of list2". It just happens that the implementation of slices in the python interpreter creates a new list for a slice. You may say it is 'obvious' that it works like that, in python, I will agree with you. You might not be able to say the same thing for other languages, especially if you haven't used them before.
The problem is, even with a simple knowledge of slices, someone reading your code may not realise that list1 is actually a new list; you are depending upon a side-effect of the language. If you used the Copy module, or even the list constructor, it would have been much clearer that list1 is a new list. This increases readability, which is very important if other people, or even yourself in the future re-read the code. Personally, I prefer readability in really long programs, and it isn't like you're programming C on an embedded device here.
I actually have to agree if you are coming to something like
a = b[:]
in code you are going to understand what it means or you are probably going to be missing a large portion of other things that are going on in the python code.
With that said I must also state that I think
a = list(b)
is much prettier and is easier to understand to me.
They need to learn it, then. Honestly, anyone who read and comprehended my first post now understands slice notation (with the exception of the more rarely used stride argument), so it's not like it's a ton of effort. Unless the post author is claiming that slice notation itself is cryptic, I don't even understand where he's coming from.
I happen to agree that it's more intuitive to use list() instead of slice notation, but back when I had no idea about slice notation and first saw it, it caused me to wonder what else could be done with it. After some reading, I learned a lot more about different slice tricks than I would have if I were just presented with list().
You can't say just looking at b[:] that its creates a copy unless you know its type e.g., if `b` is a numpy array then it doesn't copy the data and by changing b[i] you change a[i].
>If you omit the first index, the slice goes to the beginning of the list. If you omit the second index, the slice goes to the end of the list.
Unless you use the stride (third argument to the slice, the part that defaults to 1). If that is negative, then everything goes the opposite way, as in foo[::-1], the reverse of foo).
Comments
So, his argument is that copying using
is cryptic? I'd say it follows directly from how slice notation works. If you omit the first index, the slice goes to the beginning of the list. If you omit the second index, the slice goes to the end of the list. So, if you omit both indices, everything works as you'd expect it to if you understand slice notation. Anyone working with lists in Python should understand slice notation, so I fail to see the problem here.Let's look at the line:
That line means "list1 is a list that has all the elements of list2", not "list1 is a new list that has all the elements of list2". It just happens that the implementation of slices in the python interpreter creates a new list for a slice. You may say it is 'obvious' that it works like that, in python, I will agree with you. You might not be able to say the same thing for other languages, especially if you haven't used them before.The problem is, even with a simple knowledge of slices, someone reading your code may not realise that list1 is actually a new list; you are depending upon a side-effect of the language. If you used the Copy module, or even the list constructor, it would have been much clearer that list1 is a new list. This increases readability, which is very important if other people, or even yourself in the future re-read the code. Personally, I prefer readability in really long programs, and it isn't like you're programming C on an embedded device here.
His argument is that Python beginners won't be familiar with slice syntax, and if they encounter it in someone else's code they won't understand.
I actually have to agree if you are coming to something like
in code you are going to understand what it means or you are probably going to be missing a large portion of other things that are going on in the python code.With that said I must also state that I think
is much prettier and is easier to understand to me.They need to learn it, then. Honestly, anyone who read and comprehended my first post now understands slice notation (with the exception of the more rarely used stride argument), so it's not like it's a ton of effort. Unless the post author is claiming that slice notation itself is cryptic, I don't even understand where he's coming from.
I happen to agree that it's more intuitive to use list() instead of slice notation, but back when I had no idea about slice notation and first saw it, it caused me to wonder what else could be done with it. After some reading, I learned a lot more about different slice tricks than I would have if I were just presented with list().
You can't say just looking at b[:] that its creates a copy unless you know its type e.g., if `b` is a numpy array then it doesn't copy the data and by changing b[i] you change a[i].
list(a), copy.copy(a) are more explicit.
>If you omit the first index, the slice goes to the beginning of the list. If you omit the second index, the slice goes to the end of the list.
Unless you use the stride (third argument to the slice, the part that defaults to 1). If that is negative, then everything goes the opposite way, as in foo[::-1], the reverse of foo).