- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我最近看了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 之间还有其他差异,其中一些最重要的差异是:
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 语言中,接收者是一个“隐藏的”第零参数,可以使用特殊关键字如 self
、this
或 me
.)
关于python - Python 中 "sending messages to objects"的一个很好的例子是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33534264/
我尝试将 python 2.x 代码移植到 python 3。我正在努力解决的问题是 from mimetools import Message ... headers = Message(Strin
我有一个输入组件,它有三种类型的验证(required、validatorMessage、converterMessage),这个输入有自己的消息图标,整个表单有一个消息组件来显示所有组件的所有消息,
我有一个使用消息代理 (activemq) 的带有 spring 和 websockets 的 webapp。 这是我的配置类: @Configuration @EnableWebSocketMess
据我了解mbox Python 3.6 标准库中的类生成 email.message.Message 类型的旧式消息对象. 较新的类(class) email.message.EmailMessage
我使用的是mysql。 我有一个包含 userid、message_id、opened(true 或 false)、时间戳的消息表。 我希望所有未打开最近收到的 5 条消息的用户 这就是我现在拥有的:
我是 Android 的新手,发现要不断更新主视图,我必须创建一个线程来处理各种进程,然后将更新传回主视图。我决定使用 Handler 类来执行此操作。此示例中的 View 有一个用于激活代码的按钮和
我遇到了重定向符号的不同位置( > , &2 message message echo message >&2 message >&2 echo message message 对于所有表单,我得到了
我想使用 firebase 云函数发送通知,所以我尝试使用 firebase.messaging().getToken() 获取 token ,但我不断收到错误消息: TypeError: fireb
我实现了一个短信应用。现在我在使用 Oppo 设备时遇到了问题,因为无论何时收到消息,系统都会将默认应用程序更改为内置应用程序并显示此消息: For your messages security, S
我正在实现本指南:https://spring.io/guides/gs/centralized-configuration/关于Spring Cloud配置。 服务器: @EnableConfigS
我想在“匹配”之后,向所有比赛发送介绍信息。你知道一种轻松发送的方法吗?(我使用 Bluestacks) 提前致谢。问候。 最佳答案 只需传递 session.send()在session.dialo
在我们的应用程序中,我们使用 kafka 并有一个像这样的 Spring Cloud 输入流 @Component public interface SomeChannel { @Input(
这周我在通过 Node.js 库(代码相同,库版本相同等)向我的 iOS 设备发送消息时遇到了很多内部错误 很难调试,因为有时它会起作用。当我使用 for 循环发送 10 条消息时,我的设备将收到 3
我目前正在记录错误并希望获得尽可能多的描述性细节。我知道我可以捕获许多不同类型的异常,但 Exception.Message 之间有什么区别?和 Exception.InnerException.Me
我创建了一个映射到 MyBean.beansField 的表单。我使用 javax.validation.NotNull 注释来确保必须输入它。到目前为止一切正常,但错误消息如下所示: beansFi
我正在研究 Azure 服务总线。我的服务总线队列正在处理一条消息 3 次。我的消息锁定时间是5分钟。每条消息最多处理 2 分钟,但我不知道为什么队列会选择相同的消息并发送到处理,而重复的消息仅在 5
我正在使用最新的快速修复版本,即 1.6.0。我已经有针对 1.5.3 编写的代码,我想做的是将其升级到 1.6.0 我遇到的问题是,当我使用破解(msg,sessionID)方法时,它会抛出quic
当我调用 grails message()函数来查找和评估消息 key 对,它无法评估参数。 在我的 Controller 中,我调用消息函数: rejectWithError(message(cod
我使用一个小型 Spring 应用程序,其中数据库中的值很少,我想使用可变调用来检索它们。 API 在这里, @RestController @RequestMapping("/api/v1/prod
我在想在用以更好的方式,像这样: Please inform us about: 这个想法是以一种不同的方式向用户展示消息,具有更多的风格。这可能吗? 最佳答案 它们可通过 Fac
我是一名优秀的程序员,十分优秀!