为什么Python中有可调用对象?

发布于 2024-08-24 14:11:32 字数 28 浏览 7 评论 0原文

可调用对象的目的是什么?他们解决什么问题?

What is the purpose of a callable object? What problems do they solve?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一梦浮鱼 2024-08-31 14:11:32

在 Python 中,许多类型的对象都是可调用的,它们可以用于多种目的:

  • 函数是可调用的,它们可能带有来自外部函数的“闭包”
  • ,类是可调用的,调用类可以获取该类的实例
  • 。可调用,用于专门与实例相关的类似函数的行为。
  • 静态方法和类方法是可调用的,用于类似方法的功能,当
    功能在某种意义上属于“整个类”(静态方法的用处是
    可疑,因为类方法也可以这样做;-)
  • 生成器是可调用的,调用生成器
  • 最终会得到一个迭代器对象,这可能正是您所询问的(没有意识到)
    以上所有内容也是对象...!!!),您可以编写一个类,其实例
    可调用:这通常是调用更新实例的最简单方法
    状态并依赖于它(尽管函数具有合适的闭包和边界
    方法,提供替代方案,当您需要时,可调用实例是一种方法
    对同一对象执行调用一些其他特定操作:
    例如,您希望能够调用一个对象,但最好也对其应用索引
    是可调用和可索引的类的实例;-)。

Python 标准库提供了大量“它们解决的问题”的示例,其中包含我上面提到的每种特定类型的许多案例。

Many kinds of objects are callable in Python, and they can serve many purposes:

  • functions are callable, and they may carry along a "closure" from an outer function
  • classes are callable, and calling a class gets you an instance of that class
  • methods are callable, for function-like behavior specifically pertaining to an instance
  • staticmethods and classmethods are callable, for method-like functionality when the
    functionality pertains to "a whole class" in some sense (staticmethods' usefulness is
    dubious, since a classmethod could do just as well;-)
  • generators are callable, and calling a generator gets you an iterator object
  • finally, and this may be specifically what you were asking about (not realizing that
    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.

独享拥抱 2024-08-31 14:11:32

它们接受参数并根据这些参数返回结果。

可调用只是函数和接口的抽象形式,定义对象的行为类似于函数(即接受参数)。

由于函数是第一类对象,因此显然函数是可调用对象。如果您正在谈论 __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.

表情可笑 2024-08-31 14:11:32

在某些区域,特别是在“函数调用函数的函数”中,对象允许较少的嵌套。

考虑制作一个经典的装饰器,在调用函数之前检查授权级别。使用它很清楚:

@check_authorization(level="Manager")
def update_price(Item, new_price):...

您可以将其作为嵌套函数来执行:

def check_authorization(level):
     def take_params(function):
         def concrete(*args, **kwargs):
             if user_level_greater_than(level):
                 return function(*args, 
                     **kwargs)
             return None
         return concrete
     return take_params

或者您可以将其作为一个类,这可能更清晰:

  class check_authorization(object):
      def __init__(level):
         self.level = level
      def __call__(function):
          self.function = function
          return self.dec
      def dec(self, *args, **kwargs):
          if user_level_greater_than(self.level):
             return self.function(*args,v**kwargs)
          return None

许多人会发现这种平面方法更清晰。当然,我相信作弊,因为我喜欢正确的签名和元数据:

from dectools.dectools import make_call_if

@make_call_if
def check_authorization(function, arg, kwargs, level):
    return user_level_greater_than(level)

可调用对象是一种工具,它对某些已知的应用程序很有用,也可能对解决现实生活中向您抛出的奇怪问题有好处。

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:

@check_authorization(level="Manager")
def update_price(Item, new_price):...

You could do this as nested functions:

def check_authorization(level):
     def take_params(function):
         def concrete(*args, **kwargs):
             if user_level_greater_than(level):
                 return function(*args, 
                     **kwargs)
             return None
         return concrete
     return take_params

Or you could to this as a class, which might be clearer:

  class check_authorization(object):
      def __init__(level):
         self.level = level
      def __call__(function):
          self.function = function
          return self.dec
      def dec(self, *args, **kwargs):
          if user_level_greater_than(self.level):
             return self.function(*args,v**kwargs)
          return None

Many would find this flat method more clear. Of course, I believe in cheating, because I like the signatures and metadata correct:

from dectools.dectools import make_call_if

@make_call_if
def check_authorization(function, arg, kwargs, level):
    return user_level_greater_than(level)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文