今天再看python的官方文档,有一些地方说“。。。defines the following functions”然后有一些地方说“The following methods”。
今天再看python的官方文档,有一些地方说“。。。defines the following functions”然后有一些地方说“The following methods”。
|
1
定義在 Class 裡面的叫作 method(方法)
直接定義在程式碼裡的叫作 function(函數) |
|
dacapodayJul 25, 2015
2
含有self参数的函数就是方法,我瞎掰的
|
|
FrankFang128Jul 25, 2015
3
方法是 OOP 的概念,函数是一般概念
|
|
laoyuanJul 25, 2015
4
sorted(list) 和 list.sort( ) 什么区别?
sorted(list) 创建了一个新的对象,list.sort( ) 仅仅是修改list 所指向的数据对象 举个栗子: l = [3, 2, 1] def f(l): l.sort() f(l) print l |
|
jimzhongJul 25, 2015
5
有区别的,对于类是method,method也是一种函数把
|
|
6
python2:
class中定义的def称为method,形参中有self的为绑定方法,无self的为未绑定方法;class之外定义的def称为function python3: 取消了未绑定方法,统一归入function |
|
publicID001Jul 25, 2015 via Android
7
@echo1937 bound method 和 unbound method 的区别并不在 self 参数上面,self 只是一个名字而已,用来代表被传入方法的实例(事实上你把所有的 self 全改成 this 代码也一样能运行)
对于某个特定的类的实例方法(比如 Foo.bar),Foo.bar 是 unbound method,foo.bar 是 bound method |
|
publicID001Jul 25, 2015 via Android
8
@echo1937 当然 Python 3 确实是取消了 unbound method 这个设定
|
|
MetroWindJul 26, 2015
9
就是个名字的规定而已,面向对象的习惯叫法。其实都是函数。
|
|
kamushinJul 26, 2015
10
区别在于,实例会把自己传入方法。
|
|
11
我就以Python3为例子好了。
首先呢,是有Method和Function的区别,他们的类型都不一样。function就是function,method就是method。 其次呢,function可以通过descriptor protcol得到method,method呢也可以从__func__得到原本的function。 (记忆这样告诉我,现在没空翻CPython源代码确认) Python的一大精髓就是descriptor protocol,理解清楚这个了,这个问题的答案自然就揭晓了。 Let the C code speak. ;) |
|
0zero0Jul 26, 2015
12
|