简体   繁体   English

使用面向对象的方法将函数作为参数传递的等效条件是什么

[英]What is the equivalent of passing functions as arguments using an object oriented approach

I have a program in python that includes a class that takes a function as an argument to the __init__ method. 我在python中有一个程序,其中包含一个类,该类将函数作为__init__方法的参数。 This function is stored as an attribute and used in various places within the class. 此函数存储为属性,并在类中的各个位置使用。 The functions passed in can be quite varied, and passing in a key and then selecting from a set of predefined functions would not give the same degree of flexibility. 传入的函数可以有很大的不同,传入一个键然后从一组预定义的函数中进行选择不会提供相同程度的灵活性。

Now, apologies if a long list of questions like this is not cool, but... 现在,道歉,如果一长串这样的问题不是很酷,但是...

  • Is their a standard way to achieve this in a language where functions aren't first class objects? 他们是在函数不是一流对象的语言中实现此目标的标准方法吗?
  • Do blocks, like in smalltalk or objective-C, count as functions in this respect? 在这方面,像smalltalk或objective-C这样的模块是否可以算作功能?
  • Would blocks be the best way to do this in those languages? 在这些语言中,区块是做到这一点的最佳方法吗?
  • What if there are no blocks? 如果没有障碍怎么办?
  • Could you add a new method at runtime? 您可以在运行时添加新方法吗?
  • In which languages would this be possible (and easy)? 可以使用哪些语言(且容易)?
  • Or would it be better to create an object with a single method that performs the desired operation? 还是用执行所需操作的单个方法创建对象会更好?
  • What if I wanted to pass lots of functions, would I create lots of singleton objects? 如果我想传递许多函数该怎么办,我会创建许多单例对象吗?
  • Would this be considered a more object oriented approach? 这会被认为是一种更加面向对象的方法吗?
  • Would anyone consider doing this in python, where functions are first class objects? 有人会考虑在python中做到这一点的吗,函数是第一类对象?

I'm just going to answer some of your questions. 我将回答您的一些问题。

As they say in the Scheme community, "objects are a poor man's closures" (closures being first-class functions). 正如他们在Scheme社区中所说的那样,“对象是穷人的封闭”(封闭是一流的功能)。 Blocks are usually just syntactic sugar for closures. 块通常只是用于关闭的语法糖。 For languages that do not have closures, there exist various solutions. 对于没有闭包的语言,存在各种解决方案。

One of the common solutions is to use operator overloading: C++ has a notion of function objects, which define a member operator() ("operator function call"). 常见的解决方案之一是使用运算符重载:C ++具有函数对象的概念,这些对象定义了一个成员operator() (“运算符函数调用”)。 Python has a similar overloading mechanism, where you define __call__ : Python具有类似的重载机制,您可以在其中定义__call__

class Greeter(object):
    def __init__(self, who):
         self.who = who

    def __call__(self):
         print("Hello, %s!" % who)

hello = Greeter("world")
hello()

Yes, you might consider using this in Python instead of storing functions in objects, since functions can't be pickled . 是的,您可能会考虑在Python中使用它,而不是将函数存储在对象中,因为不能对函数进行腌制

In languages without operator overloading, you'll see things like Guava's Function interface. 在没有运算符重载的语言中,您会看到类似Guava的Function接口的内容。

You could use the strategy pattern . 您可以使用策略模式 Basically you pass in an object with a known interface, but different behavior. 基本上,您传入具有已知接口但行为不同的对象。 It's like passing function but one that's wrapped up in an object. 这就像传递函数,但是包裹在一个对象中。

I don't understand what you mean by "equivalent... using an object oriented approach". 我不明白您所说的“等效...使用面向对象的方法”是什么意思。 In Python, since functions are (as you say) first-class objects, how is it not "object-oriented" to pass functions as arguments? 在Python中,由于函数(如您所说)是一类对象,因此将函数作为参数传递是不是“面向对象”的呢?

a standard way to achieve this in a language where functions aren't first class objects? 用一种不是功能不是一流对象的语言来实现这一目标的标准方法?

Only to the extent that there is a standard way of functions failing to be first-class objects, I would say. 我会说,仅在某种程度上,标准的功能无法成为一流的对象。

In C++, it is common to create another class, often called a functor or functionoid , which defines an overload for operator() , allowing instances to be used like functions syntactically. 在C ++中,通常会创建另一个类,通常称为functorfunctionoid ,该类定义operator()的重载,从而允许实例在语法上像函数一样使用。 However, it's also often possible to get by with plain old function-pointers. 但是,通常也可以使用简单的旧函数指针。 Neither the pointer nor the pointed-at function is a first-class object, but the interface is rich enough. 指针和指向函数都不是一流的对象,但是接口足够丰富。

This meshes well with "ad-hoc polymorphism" achieved through templates; 这与通过模板实现的“临时多态性”非常吻合。 you can write functions that don't actually care whether you pass an instance of a class or a function pointer. 您可以编写实际上不关心是否传递类实例或函数指针的函数。

Similarly, in Python, you can make objects register as callable by defining a __call__ method for the class. 同样,在Python中,您可以通过为该类定义__call__方法来使对象注册为callable对象。

Do blocks, like in smalltalk or objective-C, count as functions in this respect? 在这方面,像smalltalk或objective-C这样的模块是否可以算作功能?

I would say they do. 我会说他们这样做。 At least as much as lambdas count as functions in Python, and actually more so because they aren't crippled the way Python's lambdas are. 至少lambda与Python中的函数一样多,实际上更多的是,因为它们不会像Python的lambda那样残缺。

Would blocks be the best way to do this in those languages? 在这些语言中,区块是做到这一点的最佳方法吗?

It depends on what you need. 这取决于您的需求。

Could you add a new method at runtime? 您可以在运行时添加新方法吗? In which languages would this be possible (and easy)? 可以使用哪些语言(且容易)?

Languages that offer introspection and runtime access to their own compiler. 提供自检和运行时访问其自己的编译器的语言。 Python qualifies. Python合格。

However, there is nothing about the problem, as presented so far, which suggests a need to jump through such hoops. 但是,到目前为止,这个问题还没有解决,这表明需要跳过这些障碍。 Of course, some languages have more required boilerplate than others for a new class. 当然,对于一门新课程,某些语言比其他语言需要更多样板。

Or would it be better to create an object with a single method that performs the desired operation? 还是用执行所需操作的单个方法创建对象会更好?

That is pretty standard. 那是很标准的。

What if I wanted to pass lots of functions, would I create lots of singleton objects? 如果我想传递许多函数该怎么办,我会创建许多单例对象吗?

You say this as if you might somehow accidentally create more than one instance of the class if you don't write tons of boilerplate in an attempt to prevent yourself from doing so. 您这样说,就好像您不编写大量模板以防止自己这样做时,不知何故会意外创建该类的多个实例。

Would this be considered a more object oriented approach? 这会被认为是一种更加面向对象的方法吗?

Again, I can't fathom your understanding of the term "object-oriented". 同样,我无法理解您对术语“面向对象”的理解。 It doesn't mean "creating lots of objects". 这并不意味着“创建许多对象”。

Would anyone consider doing this in python, where functions are first class objects? 有人会考虑在python中做到这一点的吗,函数是第一类对象?

Not without a need for the extra things that a class can do and a function can't. 并非不需要类可以做而函数不能做的额外事情。 With duck typing, why on earth would you bother? 用鸭子打字,为什么你要打扰?

In Smalltalk you'd mostly be using blocks. 在Smalltalk中,您通常会使用块。 You can also create classes and instances at runtime. 您也可以在运行时创建类和实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM