Why is the sort method of the list in Python sure to return None instead of the sorted list?

Source: Internet
Author: User
Tags sorts
The feeling of returning to none is simply anti-human, so how many people jumped into this hole with no hesitation
Sorted nothing is more convenient to return the sorted list

Reply content:

In general, a return of None represents an operation on the original object. Returning a sorted result means that a copy was created.

If you need to return to a sorted order, you can use the sorted function. I feel that the Lord needs a sorted function rather than a sort. This is a good question, a lot of people have been in the pit.

1. From a design standpoint, because command-query separation
Command-query separation states that every method should either is a Command that performs an action, or a query tha T returns data to the caller and not both. In other words, asking a question should the answer. More formally, methods should return a value of only if they is referentially transparent and hence possess no side effects.

This means that only reference transparency, which is a function without side effects, should have a return value.

X.sort (), obviously, changes the value of x, has side effects, so there should be no return value.

2. In terms of legibility, Guido van Rossum The Python-dev mailing list also explains why the return value of X.sort () is chosen to be None. (Original message: [Python-dev] Sort () return value )

3. From a performance and usability standpoint, the Python Doc explains why this is designed (design and history faq-list sort )。
in situations where performance matters, making a copy of the list just to sort it would be wasteful. Therefore, List.sort () sorts the list in place. In order to remind your that fact, it does not return the sorted list. This is the won ' t be fooled into accidentally overwriting a list when you need a sorted copy but also need to keep the U nsorted version around.
This is not anti-human.

When a programming language provides a sort function, the first question that the user should ask is whether the function is In-place or returns a new object. Python sort is in-place, then the list after sorting is not necessary to return, of course, return none is the most correct way, what is the pit to say? The sort method of the sorted function and list is not the same

As a simple example, dog is an object, what should the Dog.bark () return? After the call is over, I return what.

But if there is a function mated, what should the mated (dog) return? The dog of the day, Mated_dog.

List.sort () is called by the list itself, the list sorts itself (self), the self is changed, and the sort action itself does not need to be returned, such as I.eat (Apple), Does this eat still need to return me?

and sorted this function, is to return the incoming object sort, the function does not have any own existence, so do not return something, it is meaningless.

In short, this problem belongs to object-oriented programming, do not use functional programming of the thinking of hard sets.

PS: The above are my nonsense, I do not understand what object-oriented, functional. Because when you need it to return an array, you write it wrong.

When you write wrong, you can remind you that this is the function of returning to NULL. How many people do not hesitate to jump the hole? Oh, not much. Python also has a sorted function
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.