方法和功能之间的区别 [英] Difference between methods and functions

查看:107
本文介绍了方法和功能之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做Code Academy的Python教程,我对方法和函数的定义有些困惑。从教程:

I'm doing Code Academy's tutorials on Python, and I'm a bit confused about the definition of method and function. From the tutorial:


您已经了解了我们用于(或创建)字符串的一些内置函数,例如作为 .upper() .lower() str() len()

来自C ++,我会认为 .upper() .lower()将被调用方法并且 len() str()函数。在教程中,这些术语似乎可以互换使用。

Coming from C++, I would think .upper() and .lower() would be called methods and len() and str() functions. In the tutorial, the terms seem to be used interchangeably.

Python是否以C ++的方式区分方法和函数?

Does Python distinguish between methods and functions in the way C++ does?

方法和函数之间的区别不同,我在询问Python的详细信息。术语'方法'和'函数'似乎并不总是遵循链接问题的接受答案中给出的定义。

Unlike Difference between a method and a function, I'm asking about the particulars of Python. The terms 'method' and 'function' do not seem to always follow the definition given in the accepted answer of the linked question.

推荐答案

function 是Python中的可调用对象,即可以使用调用操作符调用(尽管其他对象可以通过执行 __ call__来模拟函数)。例如:

A function is a callable object in Python, i.e. can be called using the call operator (though other objects can emulate a function by implementing __call__). For example:

>>> def a(): pass
>>> a
<function a at 0x107063aa0>
>>> type(a)
<type 'function'>

方法是一个特殊的函数类,可以是 bound unbound

A method is a special class of function, one that can be bound or unbound.

>>> class A:
...   def a(self): pass
>>> A.a
<unbound method A.a>
>>> type(A.a)
<type 'instancemethod'>

>>> A().a
<bound method A.a of <__main__.A instance at 0x107070d88>>
>>> type(A().a)
<type 'instancemethod'>

当然,不能调用一个未绑定的方法(至少不是直接传递实例作为参数) :

Of course, an unbound method cannot be called (at least not directly without passing an instance as argument):

>>> A.a()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method a() must be called with A instance as first argument (got nothing instead)



在Python中,在大多数情况下,您不会注意到绑定方法,函数或可调用对象(即实现 __ call __ 的对象)之间的区别,或者一个类的构造函数。它们看起来都一样,它们只是有不同的命名约定。尽管如此,这些对象可能看起来有很大不同。

In Python, in most cases you won't notice the difference between a bound method, a function or a callable object (i.e. an object that implements __call__), or a class constructor. They all look the same, they just have different naming conventions. Under the hood, the objects may look vastly different though.

这意味着绑定方法可以用作函数,这是使许多小事情中的一个Python如此强大

This means that a bound method can be used as a function, this is one of the many small things that makes Python so powerful

>>> b = A().a
>>> b()

这也意味着即使 len(...) str(...)(后者是一个类型构造函数),您将不会注意到您可以深入一点:

It also means that even though there is a fundamental difference between len(...) and str(...) (the latter is a type constructor), you won't notice the difference until you dig a little deeper:

>>> len
<built-in function len>
>>> str
<type 'str'>

这篇关于方法和功能之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆