Python里method和function的区别
2010-12-01 15:09
423 查看
首先来看他们的定义,
函数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).
首先,从概念上去分。函数可以看成一个数学上的概念,比如说完成加减乘除的函数。它其实有一个内在的约束:就是如果参数相同,对一个函数的每次调用返回的结果应该始终一样。
而方法是要和对象联系起来的。而且它有一个银行的参数:就是它所作用的对象。而这些更多地是在面向对象的语境里来说。
而从Python的定义来看,它将method作为function的一种特例来处理了。但当你在文档里看到instance method object和function object的说法时,你应该了解他们的差异。
从上面的例子可以看出,一个是method object,一个是function object。
此外,Python里面还有staticmethod和classmethod的概念,他们和普通的method又有些区别。
->classmethod将类作为第一个隐含的参数
->而staticmethod没有任何隐含的参数,有点类似于Java里的static
当然,在很多语言里,这两者的区别没那么明显。
函数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).
首先,从概念上去分。函数可以看成一个数学上的概念,比如说完成加减乘除的函数。它其实有一个内在的约束:就是如果参数相同,对一个函数的每次调用返回的结果应该始终一样。
而方法是要和对象联系起来的。而且它有一个银行的参数:就是它所作用的对象。而这些更多地是在面向对象的语境里来说。
而从Python的定义来看,它将method作为function的一种特例来处理了。但当你在文档里看到instance method object和function object的说法时,你应该了解他们的差异。
>>> class MyClass: i = 12345 def f(self): return "hello world" >>> x = MyClass() >>> x.f.__class__ <class 'method'> >>> MyClass.f.__class__ <class 'function'>
从上面的例子可以看出,一个是method object,一个是function object。
此外,Python里面还有staticmethod和classmethod的概念,他们和普通的method又有些区别。
->classmethod将类作为第一个隐含的参数
->而staticmethod没有任何隐含的参数,有点类似于Java里的static
>>> class MyClass: i = 12345 @staticmethod def f(self): return "hello world" def add(x, y): return x+y @classmethod def foo(cls): print(cls) >>> MyClass.f.__class__ <class 'function'> >>> MyClass.add.__class__ <class 'function'> >>> MyClass.foo.__class__ <class 'method'> >>> x = MyClass() >>> x.f.__class__ <class 'function'> >>> x.add.__class__ <class 'method'> >>> x.foo.__class__ <class 'method'> >>> x.foo() <class '__main__.MyClass'>
当然,在很多语言里,这两者的区别没那么明显。
相关文章推荐
- 我所理解的Python里method和function的区别(关键词:method/方法/function/函数)
- Python函数(function)与方法(method)区别
- Function & Method的区别 为什么类中成员函数的指针需要显式地声明?
- 方法(method)和函数(function)的区别
- python中@staticmethod和@classmethod的作用与区别
- python - @staticmethod和@classmethod的作用与区别
- PHP中__FUNCTION__与__METHOD__的区别
- Python: classmethod & staticmethod 区别
- PHP 魔术常量__FUNCTION__与__METHOD__的区别
- Python @classmethod和@staticmethod区别
- python中staticmethod classmethod及普通函数的区别
- Python: classmethod & staticmethod 区别
- python @classmethod 和 @staticmethod区别,以及类中方法参数cls和self的区别
- method和function的区别
- PHP的method_exists,function_exists,is_callable的区别
- python TypeError: 'builtin_function_or_method' object is not iterable keys
- python @staticmethod @classmethod 区别
- Effective JavaScript Item 18 理解Function, Method, Constructor调用之间的区别
- python the method of super function
- Python中function和method