与C ++相比,Python中方法和函数之间的差异 [英] Difference between methods and functions, in Python compared to C++

查看:59
本文介绍了与C ++相比,Python中方法和函数之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写有关Python的Code Academy教程,对方法和函数的定义有些困惑.在本教程中:

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.

推荐答案

函数是Python中的可调用对象,即可以使用 call运算符进行调用(尽管其他对象可以通过实现__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'>

方法是一类特殊的函数,可以绑定 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'>

这篇关于与C ++相比,Python中方法和函数之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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