gpt4 book ai didi

python - Python 中 "sending messages to objects"的一个很好的例子是什么?

转载 作者:太空狗 更新时间:2023-10-29 22:03:29 26 4
gpt4 key购买 nike

我最近看了Nothing is Something由 Sandi Metz 撰写,在她的演讲中,她使用了向对象发送消息 的想法,并介绍了如何在 Ruby 中完成此操作。 4:10 -7:30 部分将是她开始讨论主题的一个很好的切入点(它是一个构建 block ,然后渗透到谈话的一半以上)。

现在,对于一些背景知识:我没有很多用 Ruby 编写程序的经验,对 smalltalk 的经验也为零。我的 OO 经验有些有限而且非常陈旧。我还在 Google 中查找了 send object message python,我所看到的都是与通过套接字和电子邮件发送消息有关,这与我的想法不符。

我不确定如何在 Python 中解释这个概念,或者如何实现它。有什么想法吗? :)


旁注:她提到她的 OO 观点来自 smalltalk 的经验,所以我将其添加为这个问题的标签。

最佳答案

Python 使用的术语略有不同。它被称为“调用方法”。但这是一回事。 (C++ 称之为“调用虚函数”。同样,区别相同。)

就我个人而言,我不喜欢这个术语,它过于关注实现细节而失去了“消息发送”术语的大部分隐喻能力。

与 Python 之间还有其他差异,其中一些最重要的差异是:

  • 面向对象的数据抽象是通过约定实现的,而不是作为内置语言功能(例如 Smalltalk、Ruby)或设计模式(Scheme、ECMAScript)
  • 不是所有的子程序都是方法

OO 的基本思想是消息传递:您向对象发送消息,对象响应。就像在现实生活中一样,您不知道对象对消息做了什么。您只能观察到答复。该对象可能会自己处理消息,它可能会借助其他人的帮助,它可能会盲目地转发消息而自己实际上没有做任何工作。

由于您无法知道对象对消息做了什么,您只能观察到对象的响应,因此您对对象的了解就是它的协议(protocol)(它理解的消息以及它如何理解回应他们)。你不知道它的实现,你不知道它的表示。这就是 OO 实现数据抽象、信息隐藏、数据隐藏、封装的方式。

此外,由于每个对象都独立决定如何响应消息,因此您获得了多态性。

一种响应消息的典型方式是执行与该消息对应的方法。但这是一种实现机制,这就是我不喜欢该术语的原因。作为比喻,它没有我上面提到的任何含义。

Alan Kay has said that OO is about three things, Messaging, Data Abstraction, and Polymorphism :

OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things.

他后来澄清说the Big Thing is Messaging :

Just a gentle reminder that I took some pains at the last OOPSLA to try to remind everyone that Smalltalk is not only NOT its syntax or the class library, it is not even about classes. I'm sorry that I long ago coined the term "objects" for this topic because it gets many people to focus on the lesser idea.

The big idea is "messaging" -- that is what the kernal of Smalltalk/Squeak is all about (and it's something that was never quite completed in our Xerox PARC phase). The Japanese have a small word -- ma -- for "that which is in between" -- perhaps the nearest English equivalent is "interstitial". The key in making great and growable systems is much more to design how its modules communicate rather than what their internal properties and behaviors should be. Think of the internet -- to live, it (a) has to allow many different kinds of ideas and realizations that are beyond any single standard and (b) to allow varying degrees of safe interoperability between these ideas.

事实上,正如我上面所说,在我看来,其他两个只是消息传递的结果。

当 Alan Kay 提出“面向对象”一词时,他深受后来成为 ARPANet 和 Internet 的东西的启发:具有自己的私有(private)内存(“实例变量”)的独立机器(“对象”)通过发送消息相互通信。

On Understanding Data Abstraction, Revisited中也有类似的观点通过 William R. Cook还有他的Proposal for Simplified, Modern Definitions of "Object" and "Object Oriented" .

Dynamic dispatch of operations is the essential characteristic of objects. It means that the operation to be invoked is a dynamic property of the object itself. Operations cannot be identified statically, and there is no way in general to exactly what operation will executed in response to a given request, except by running it. This is exactly the same as with first-class functions, which are always dynamically dispatched.

Python 的对象系统与其他语言有点不同。 Python 最初是一种过程式语言,后来添加了对象系统,目的是使语言的更改量尽可能少。 Python 中的主要数据结构是 dict(映射/哈希表),所有行为都在函数中。甚至在 Python 的 OO 特性出现之前,这种极简主义就表现出来了,例如局部变量和全局变量实际上只是 dict 中的键。因此,很自然地使对象和类很像 dict 并重用该概念,对象本质上是值的 dict,而类是 dict 函数。没有单独的“方法”概念,相反,您拥有将接收者作为第一个参数的函数。 (在大多数其他 OO 语言中,接收者是一个“隐藏的”第零参数,可以使用特殊关键字如 selfthisme.)

关于python - Python 中 "sending messages to objects"的一个很好的例子是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33534264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com