3 ms·
> In Python 3, I have to tell my students that > breakfast = ["bacon", "eggs"] > sorted_breakfast = breakfast.sort() > works Well, some of your students
by narimiran 9y ago
> In Python 3, I have to tell my students that
> breakfast = ["bacon", "eggs"]
> sorted_breakfast = breakfast.sort()
> works
Well, some of your students might point out to you that your `sorted_breakfast` is `None`, because that is what `sort()` returns, and even this example doesn't work as you have wanted ;)
On the other hand, if you have used `sorted(breakfast)` (and `sorted(breakfast.keys())` in other example), both examples would work as intended, and you couldn't blame Python 3.
> "Why is sorted() rather than sort()" - "just because."
It is not "just because", it is because it returns a sorted list (which is exactly what you wanted), compared to returning None.
- masklinn 9y ago> It is not "just because", it is because it returns a sorted list (which is exactly what you wanted), compared to returning None. Also because `sorted()` works on any iterable, whereas .sort only works on lists.
- xapata 9y agoI know that the best way to learn is "see one, do one, teach one" but I wish you'd put more effort into learning the language before teaching it. The python.org tutorial and FAQ have answers to these questions. I think it's unethical to tell a student, "just because" instead of, "I don't know why."