在Python中,對這兩個東西有明確的規定:
函數function —— A series of statements which returns some value to a caller. It can also be passed zero or more arguments which may be used in the execution of the body.
方法method —— A function which is defined inside a class body. If called as an attribute of an instance of that class, the method will get the instance object as its first argument (which is usually called self).
從定義的角度上看,我們知道函數(function)就是一系列語句,這些語句返回給調用者一些值或None,它理論上不與其它東西關系,它只需要相關的參數就可以。所以普通的在模塊中定義的稱謂函數是很有道理的。
那么方法的意思就很明確了,它是與某個對象相互關聯的,也就是說它的實現與某個對象有關聯關系。這就是方法。雖然它的定義方式和函數是一樣的。
那明確了兩者的區別,那我們怎么讓電腦識別是function還是methon
