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

时间:2022-05-14 01:24:17

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

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

3 个解决方案

#1


7  

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

callable仅仅是一个函数resp接口的抽象形式,它定义了一个对象的行为就像一个函数(即接受参数)。

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.

由于函数是第一类对象,显然函数是可调用对象。如果您正在讨论__call__方法,这只是您可以重载自定义对象行为的许多特殊方法中的一种,例如,对于算术操作,或者定义如果您调用一个对象会发生什么。

One idea why to use such is to have some kind of factory object that itself creates other objects.

为什么要使用这样的一种方法,就是拥有某种自己创建其他对象的工厂对象。

#2


12  

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

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

  • 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.

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

#3


1  

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:

考虑创建一个经典的decorator,在调用函数之前检查授权级别。使用它是明确的:

@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.

callable对象是一种工具,它对一些已知的应用程序很有用,也可能对现实生活中遇到的奇怪问题有好处。

#1


7  

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

callable仅仅是一个函数resp接口的抽象形式,它定义了一个对象的行为就像一个函数(即接受参数)。

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.

由于函数是第一类对象,显然函数是可调用对象。如果您正在讨论__call__方法,这只是您可以重载自定义对象行为的许多特殊方法中的一种,例如,对于算术操作,或者定义如果您调用一个对象会发生什么。

One idea why to use such is to have some kind of factory object that itself creates other objects.

为什么要使用这样的一种方法,就是拥有某种自己创建其他对象的工厂对象。

#2


12  

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

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

  • 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.

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

#3


1  

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:

考虑创建一个经典的decorator,在调用函数之前检查授权级别。使用它是明确的:

@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.

callable对象是一种工具,它对一些已知的应用程序很有用,也可能对现实生活中遇到的奇怪问题有好处。