我想知道在 python 中 sum()、min() 和 max() 是在哪里定義的?例如執行此操作:
a = [1,2,3,4]
min(a)
如果我尋找串列 min 的方法不存在:
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
uj5u.com熱心網友回復:
它們是內置函式:
Python 解釋器內置了許多始終可用的函式和型別。它們在此處按字母順序列出。
內置函式
...
米
map()
max()
memoryview()
min()...
如果它們是 on 的方法list,您可以將它們稱為a.min()and a.max()。
這些函式是全域函式而不是方法的原因是它們更通用:它們適用于任何可迭代物件。例如,您也可以呼叫min(x for x in a if x%2 == 0),而是將其傳遞給生成器。
uj5u.com熱心網友回復:
sum 函式不是串列物件的方法,就像您在那里展示的那樣,而是一個全域函式。
它內置在python中。見這里:https ://docs.python.org/3/library/functions.html?msclkid=c061f6fbc53b11ec9ef2424170aeda56#sum
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/464514.html
標籤:Python python-3.x 列表 数据结构
