Python的函数是对象, [英] Python 'Functions are objects'
问题描述
我总是听到这个声明在Python(对于主题,如装饰器等,当你传递函数,等),但从来没有真正看到这方面的细节。
I always hear this statement in python (for topics such as decorators, etc. when you are passing functions, etc.) but have never really seen an elaboration on this.
例如,可以创建一个只有一个抽象方法的类 c
For example is it possible to create a class c
that has only one abstract method that is called with a set of opened and closed brackets.
i.e class c:
@abstractmethod
def method_to_be_called_by():
...
以便您可以拥有
c(whatever parameters are required)
(我不会是第一次,也不会是最后一次),我只是好奇这个
I could be way off the mark with my understanding here(wouldn't be the first time and won't be the last), I was just curious about what people meant by this
推荐答案
您正在查找 __ call __
方法。函数对象有这种方法:
You are looking for the __call__
method. Function objects have that method:
>>> def foo(): pass
...
>>> foo.__call__
<method-wrapper '__call__' of function object at 0x106aafd70>
不是说Python解释器循环实际上在遇到Python函数对象时使用该方法;
Not that the Python interpreter loop actually makes use of that method when encountering a Python function object; optimisations in the implementation jump straight to the contained bytecode in most cases.
但你可以 在你自己的自定义类上使用:
But you can use that on your own custom class:
class Callable(object):
def __init__(self, name):
self.name = name
def __call__(self, greeting):
return '{}, {}!'.format(greeting, self.name)
演示:
>>> class Callable(object):
... def __init__(self, name):
... self.name = name
... def __call__(self, greeting):
... return '{}, {}!'.format(greeting, self.name)
...
>>> Callable('World')('Hello')
'Hello, World!'
当您使用 创建函数对象 > def
语句,或,您可以使用 lambda
表达式:
Python creates function objects for you when you use a def
statement, or you use a lambda
expression:
>>> def foo(): pass
...
>>> foo
<function foo at 0x106aafd70>
>>> lambda: None
<function <lambda> at 0x106d90668>
您可以将其与使用文字语法创建字符串或整数或列表进行比较:
You can compare this to creating a string or an integer or a list using literal syntax:
listobject = [1, 'two']
上面的代码创建了3个对象而不调用某个类型,Python根据所使用的语法为您创建了所有对象。这同样适用于函数。
The above creates 3 objects without ever calling a type, Python did that all for you based on the syntax used. The same applies to functions.
创建一个自己可以有点复杂;您需要有一个代码对象并引用一个全局命名空间,至少:
Creating one yourself can be a little more complex; you need to have a code object and reference to a global namespace, at the very least:
>>> function_type = type(lambda: None)
>>> function_type
<type 'function'>
>>> function_type(foo.__code__, globals(), 'bar')
<function bar at 0x106d906e0>
这里我通过重用函数
类型,从 foo
函数获取代码对象;函数类型不是内置的名称,但类型确实存在,可以通过在现有函数实例上调用 type()
获得。
Here I created a function object by reusing the function
type, taking the code object from the foo
function; the function type is not a built-in name but the type really does exist and can be obtained by calling type()
on an existing function instance.
我也在我的解释器的全局命名空间中传递了一个名字,后者是一个可选参数;则该名称将从代码对象中获取。
I also passed in the global namespace of my interpreter, and a name; the latter is an optional argument; the name is otherwise taken from the code object.
这篇关于Python的函数是对象,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!