为什么Python中有可调用对象?
可调用对象的目的是什么?他们解决什么问题?
What is the purpose of a callable object? What problems do they solve?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
可调用对象的目的是什么?他们解决什么问题?
What is the purpose of a callable object? What problems do they solve?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
在 Python 中,许多类型的对象都是可调用的,它们可以用于多种目的:
功能在某种意义上属于“整个类”(静态方法的用处是
可疑,因为类方法也可以这样做;-)
以上所有内容也是对象...!!!),您可以编写一个类,其实例是
可调用:这通常是调用更新实例的最简单方法
状态并依赖于它(尽管函数具有合适的闭包和边界
方法,提供替代方案,当您需要时,可调用实例是一种方法
对同一对象执行调用和一些其他特定操作:
例如,您希望能够调用一个对象,但最好也对其应用索引
是可调用和可索引的类的实例;-)。
Python 标准库提供了大量“它们解决的问题”的示例,其中包含我上面提到的每种特定类型的许多案例。
Many kinds of objects are callable in Python, and they can serve many purposes:
functionality pertains to "a whole class" in some sense (staticmethods' usefulness is
dubious, since a classmethod could do just as well;-)
all of the above are objects too...!!!), you can code a class whose instances are
callable: this is often the simplest way to have calls that update an instance's
state as well as depend on it (though a function with a suitable closure, and a bound
method, offer alternatives, a callable instance is the one way to go when you need to
perform both calling and some other specific operation on the same object: for
example, an object you want to be able to call but also apply indexing to had better
be an instance of a class that's both callable and indexable;-).
A great range of examples of the kind of "problems they solve" is offered by Python's standard library, which has many cases of each of the specific types I mention above.
它们接受参数并根据这些参数返回结果。
可调用只是函数和接口的抽象形式,定义对象的行为类似于函数(即接受参数)。
由于函数是第一类对象,因此显然函数是可调用对象。如果您正在谈论
__call__
方法,这只是您可以重载自定义对象的行为的许多特殊方法之一,例如算术运算或定义如果您调用一个对象。为什么要使用这种方法的一个想法是拥有某种工厂对象,它本身可以创建其他对象。
They take parameters and return a result depending on those parameters.
A callable is just an abstract form of a function resp an interface that defines that an object acts like a function (i.e. accepts parameters).
As functions are first class objects, it is obvious that functions are callable objects. If you are talking about the
__call__
method, this is just one of the many special methods with which you can overload the behavior of custom objects, e.g. for arithmetic operations or also defining what happens if you call an object.One idea why to use such is to have some kind of factory object that itself creates other objects.
在某些区域,特别是在“函数调用函数的函数”中,对象允许较少的嵌套。
考虑制作一个经典的装饰器,在调用函数之前检查授权级别。使用它很清楚:
您可以将其作为嵌套函数来执行:
或者您可以将其作为一个类,这可能更清晰:
许多人会发现这种平面方法更清晰。当然,我相信作弊,因为我喜欢正确的签名和元数据:
可调用对象是一种工具,它对某些已知的应用程序很有用,也可能对解决现实生活中向您抛出的奇怪问题有好处。
There are areas, especially in the 'functions calling functions of function functions' where objects allow less nesting.
Consider making a classic decorator that checks an authorization level before calling a function. Using it is clear:
You could do this as nested functions:
Or you could to this as a class, which might be clearer:
Many would find this flat method more clear. Of course, I believe in cheating, because I like the signatures and metadata correct:
The callable object is a tool which is good for some known applications and may also be good for the bizarre problem real life throws at you.