Table of Contents
回复内容:
Home Backend Development Python Tutorial 为什么 Python 中列表的 sort 方法一定要返回 None 而不是排序后的列表?

为什么 Python 中列表的 sort 方法一定要返回 None 而不是排序后的列表?

Jun 06, 2016 pm 04:23 PM
none sorted

感觉返回None简直反人类嘛,让多少人义无反顾的跳进这个坑中了
sorted无关 就是问返回排序后的列表不是更方便么

回复内容:

一般来说,返回 None 表示是在原对象上进行的操作。返回排序后结果意味着创建了一个副本。

如果你需要返回排序后的,可以使用 sorted 函数。我感觉题主需要的其实是 sorted 函数而非 sort。 这是个好问题,坑过不少人。

1. 从设计角度来说,因为 Command-query separation
Command-query separation states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, asking a question should not change the answer. More formally, methods should return a value only if they are referentially transparent and hence possess no side effects.

也就是说只有引用透明,也就是没有副作用的函数,才应该具有返回值。

x.sort(),很明显,改变了 x 的值,具有副作用,所以不应该有返回值。

2.从易读性角度来说, Guido van Rossum 在 Python-Dev 邮件列表里也解释过为什么选择将 x.sort() 的返回值定为 None. (邮件原文:[Python-Dev] sort() return value)

3. 从性能和易用性角度来说,Python Doc 解释了为什么要这样设计(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 you of that fact, it does not return the sorted list. This way, you won’t be fooled into accidentally overwriting a list when you need a sorted copy but also need to keep the unsorted version around.
这并不反人类。

一门编程语言提供了一个sort函数的时候,作为使用者第一个应该问的问题是:这个函数是in-place的还是返回一个新object。Python的sort是in-place的,那么排序完成后的列表就没有返回的必要了,当然返回None是最正确的做法,有什么坑可言? sorted函数和list的sort方法是不一样的

举个简单的例子,dog是一个对象,那dog.bark()应该返回什么?叫完了不就完了嘛,还返回啥。

但是如果有一个函数mated,那mated(dog)应该返回什么?日过的狗,mated_dog。

list.sort()是该list自身调用,list将自身(self)排序(sort)一下,自身(self)发生了改变,而排序这个动作本身是不需要返回什么的,再例如I.eat(apple),难道这个eat还需要返回一个我吗?

而sorted这个函数,是将传入的对象排序返回,这个函数没有什么自身的存在,所以不返回点什么东西,那就没意义了。

总之,这个问题属于面向对象的程序设计,不要用函数式编程的思维硬套。

PS: 以上都是我胡说的,我根本不懂什么面向对象,函数式。 因为当你需要它返回数组时,你写错了。

在你写错的时候可以提醒你,这就是返回空的作用。 让多少人义无反顾地跳坑了?呵呵没多少。 python 还有一个sorted函数
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

What are regular expressions? What are regular expressions? Mar 20, 2025 pm 06:25 PM

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

See all articles