3

以下代码在我的解释器上运行良好

a = [5, 1, 4, 3]
b = sorted(a)

为什么当我从 Jupiter 运行它时它会给我这个错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-71-4a5766271a85> in <module>()
----> 1 b = sorted(a)

TypeError: 'list' object is not callable

版本:

Server Information:
You are using IPython notebook.

The version of the notebook server is 3.2.0-8b0eef4 and is running on:
Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v.1500 64 bit (AMD64)]

Current Kernel Information:
Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v.1500 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 3.2.0 -- An enhanced Interactive Python.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
%guiref   -> A brief reference about the graphical user interface.
4

1 回答 1

1

确保名称sorted未被覆盖。

>>> sorted([5,4,3,2,1])
[1, 2, 3, 4, 5]
>>> sorted = []  # <--- overwritten; shadows builtin `sorted` function.
>>> sorted([5,4,3,2,1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
于 2015-07-31T09:46:07.653 回答