函数与方法的区别 / Distinction of Function and Method


关于函数与方法的区别,可根据两者的定义看出,

函数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).

从定义上来看,可以说,方法是一种依赖于类对象特殊函数

1 class Foo():
2     def foo(self):
3         pass
4 print(type(Foo().foo))
5 print(type(Foo.foo))
6 # print(Foo().foo.__class__)
7 # print(Foo.foo.__class__)

从输出中可以看出,脱离了类实例的方法依旧是函数(此处的 type 等价于调用了内置的 __class__ 函数)。

<class 'method'>
<class 'function'>

 

参考链接


http://blog.csdn.net/peachpi/article/details/6047826

相关文章:

  • 2022-02-15
  • 2021-08-06
  • 2021-06-20
  • 2022-12-23
  • 2021-09-13
  • 2021-07-25
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-04
  • 2022-03-04
  • 2022-01-29
  • 2022-12-23
相关资源
相似解决方案