[Python-ideas] Message passing syntax for objects

Nick Coghlan ncoghlan at gmail.com
Tue Mar 19 15:49:36 CET 2013


On Mon, Mar 18, 2013 at 11:28 PM, Chris Angelico <rosuav at gmail.com> wrote:
> There are interfaces where a generic "do something with X and Y"
> concept makes sense, but I don't think program code is one of them.

Having finally parsed out what I think the OP is asking for, I have to
disagree. In fact, Guido disagrees as well: he thinks what the OP
wants is so important that he built it into Python from day one.

The notation Python uses to "send a message" to an object is actually
"obj(message)".

This process of sending a message is referred to as "calling". When
you call someone, the message you send is referred to as "the
arguments to the call".

An object accepts messages by defining a method with the special name
"__call__".

When you define this method, you declare the "parameters" you expect
to receive as part of any calls. The process of mapping the sent
arguments to the declared parameters is referred to as "argument
binding".

You can perform the argument binding step without actually making a
call by using the inspect.Signature.bind API in Python 3.3+ (or the
backport of that API to earlier Python versions:
https://pypi.python.org/pypi/funcsigs/)

However, it's also useful to aggregate objects that you can send
messages to into larger collections, along with shared data for those
objects to work with. Python provides a "class" mechanism for this
aggregation step, and in this case the objects which receive messages
and operate on the shared data are referred to as "methods".

To prevent an infinite regress, Python has a particular kind of object
which it inherently knows how to send a message to, rather than
relying on a "__call__" method. This intrinsic message receiver is
referred to as a "function".

Regards,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list