Python中function(函数)和methon(方法)的区别

在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

 

posted @ 2017-12-20 09:58  Richie`  阅读(2527)  评论(2编辑  收藏  举报