本來是instance. methodname=types.MethodType(methodName,instance)我改成 methodname=types.MethodType(methodName,instance),即把開頭的instance. 去掉,不也能實現一樣的功能嗎,開頭的instance.是不是多余了啊,因為本來就是就是給參數2的instance綁定方法的,寫兩遍instance有意義嗎
1 回答

慕少森
TA貢獻2019條經驗 獲得超9個贊
在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)就相當于一個數學公式,它理論上不與其它東西關系,它只需要相關的參數就可以。所以普通的在module中定義的稱謂函數是很有道理的。
那么方法的意思就很明確了,它是與某個對象相互關聯的,也就是說它的實現與某個對象有關聯關系。這就是方法。雖然它的定義方式和函數是一樣的。也就是說,在Class定義的函數就是方法。
添加回答
舉報
/150
提交
取消