Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would be a good example of "sending messages to objects" within Python?

I recently watched Nothing is Something by Sandi Metz, and in her talk she uses the idea of sending messages to objects and goes over how that's done in Ruby. The 4:10-7:30 section would be a good entry point where she begins on the topic (it's a building block that then permeates over half the talk).

Now, for some background: I don't have a lot of experience with writing programs in Ruby, and zero experience with smalltalk. My OO experience is somewhat limited and very stale. I also looked up send object message python in Google, and all I saw was relating to sending messages over sockets and email, which isn't quite what I had in mind.

I'm not sure how to interpret this concept in Python, or how to implement it. Any ideas? :)


Side note: She mentions her OO-views are derived from experience with smalltalk, so I'm adding that as a tag to this question.

like image 782
wtr Avatar asked Nov 04 '15 23:11

wtr


People also ask

What is an example of an object in Python?

For example, Person(Human) can be treated as a class which has properties such as name, age,gender etc. Every individual can be treated as an object of the class human or Person. Each individual will have different values of the properties of class Person. Everyone will have different names, age and gender.

How are messages passed to objects?

In computer science, message passing is a technique for invoking behaviour (i.e., running a program) on a computer. The invoking of a program sends a message to a process (which may be an actor or object) and relies on the process and the supporting infrastructure to select and invoke the actual code to run.

What is message passing within objects?

Message passing is a technique for invoking behavior (i.e., running a program) on a computer. In contrast to the traditional technique of calling a program by name, message passing uses an object model to distinguish the general function from the specific implementations.

What are objects used for in Python?

Python Objects and Classes An object is simply a collection of data (variables) and methods (functions) that act on those data. Similarly, a class is a blueprint for that object. We can think of a class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc.


1 Answers

Python uses a slightly different terminology. It is called "calling a method". But it's the same thing. (C++ calls it "calling a virtual function". Again, same difference.)

Personally, I don't like that terminology, it focuses too much on the implementation detail and loses much of the metaphoric power of the "message sending" terminology.

There are other differences with Python, some of the most important ones being:

  • object-oriented data abstraction is achieved via convention as opposed to being a builtin language feature (e.g. Smalltalk, Ruby), or Design Pattern (Scheme, ECMAScript)
  • not all subroutines are methods

The fundamental idea of OO is messaging: you send a message to an object, the object responds. Just like in real life, you have no idea what the object does with the message. All you can observe is the reply. The object might process the message itself, it might employ the help of others, it might blindly forward the message without actually doing any work itself.

Since you can't know what the object does with the message and all you can observe is the object's response, all you know about the object is its protocol (the messages it understands and how it responds to them). You don't know its implementation, you don't know its representation. That's how OO achieves Data Abstraction, Information Hiding, Data Hiding, Encapsulation.

Also, since each object decides independently how to respond to a message, you get Polymorphism.

One typical way of responding to a message, is executing a method corresponding to that message. But that is an implementation mechanism, which is why I don't like that terminology. As a metaphor, it carries none of the connotations I mentioned above.

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.

He later clarified that 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.

And in fact, as I laid out above, the other two are just consequences of Messaging, in my opinion.

When Alan Kay came up with the term "Object Orientation", he was heavily inspired by what would later become the ARPANet and then the Internet: independent machines ("objects") with their own private memory ("instance variables") that communicate with each other by sending messages.

Similar points are also made in On Understanding Data Abstraction, Revisited by William R. Cook and also his 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's object system is a bit different from other languages. Python was originally a procedural language, the object system was added later on, with the goal of making the absolut minimal amount of changes to the language as possible. The major data structure in Python were dicts (maps / hash tables), and all behavior was in functions. Even before Python's OO features, this minimalism shows itself, e.g. local and global variables are actually just keys in a dict. And so, it was natural to make objects and classes much like dicts and reuse that concept, an object is essentially a dict of values, and a class is a dict of functions. There is no separate idea of "method", rather, you have functions which take the receiver as their first argument. (In most other OO languages, the receiver is a "hidden" zeroth argument and available using a special keyword such as self, this, or me.)

like image 195
Jörg W Mittag Avatar answered Oct 02 '22 22:10

Jörg W Mittag