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

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

问题描述

我正在做代码学院关于 Python 的教程,我对方法和函数的定义有点困惑.来自教程:

<块引用>

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

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

Python 是否像 C++ 那样区分方法和函数?

方法和函数之间的区别不同,我在询问 Python 的细节.术语方法"和函数"似乎并不总是遵循链接问题的公认答案中给出的定义.

解决方案

需要注意:这个答案似乎已经过时了.检查这个

function 是 Python 中的可调用对象,即可以使用 call 运算符 调用(尽管其他对象可以通过实现 __call__ 来模拟函数代码>).例如:

<预><代码>>>>def a(): 通过>>>一种<函数 a 在 0x107063aa0>>>>类型(一)<输入'函数'>

方法是一类特殊的函数,可以绑定未绑定.

<预><代码>>>>A类:... def a(self): 通过>>>A.a<未绑定方法A.a>>>>类型(A.a)<输入'实例方法'>>>>A().a<绑定方法A.a of <__main__.A instance at 0x107070d88>>>>>类型(A().a)<输入'实例方法'>

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

<预><代码>>>>A.a()回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.类型错误:必须使用 A 实例作为第一个参数调用未绑定的方法 a()(什么都没有)

在 Python 中,在大多数情况下,您不会注意到绑定方法、函数或可调用对象(即实现 __call__ 的对象)或类构造函数之间的区别.它们看起来都一样,只是命名约定不同.在引擎盖下,对象可能看起来大不相同.

这意味着绑定方法可以用作函数,这是使 Python 如此强大的众多小东西之一

<预><代码>>>>b = A().a>>>乙()

这也意味着即使 len(...)str(...) 之间存在根本区别(后者是类型构造函数),除非您深入挖掘,否则您不会注意到差异:

<预><代码>>>>连<内置函数len>>>>字符串<输入'str'>

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:

You already know about some of the built-in functions we've used on (or to create) strings, such as .upper(), .lower(), str(), and len().

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.

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

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.

解决方案

Needs Attention: This answer seems to be outdated. Check this

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'>

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 an 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)

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.

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()

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'>

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

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