x.y

二年前の記事を今更見つけて引っ張ってくるんだけど、x.y という表現の PythonRuby における違いについてなるほどと思ったので。Python and Ruby ({ | one, step, back | }) より。

In Python, the construct "x.y" means lookup the name "y" in the dictionary of "x". (Ok, that’s simplified, but you get the picture). The result of that lookup may be a value (instance data), or may be a function that can be called (member functions). In other words, "." is basically a lookup operation.

Python では "x.y" とは x という辞書の y という属性の検索である。結果として得られるのは値かもしれないし関数かもしれない。言い換えれば "." は基本的に「検索」の操作である。

In Ruby, the construct "x.y" means send a message named "y" to the object represented by "x". All operations on an object must be done through the sending of a message.

Ruby では "x.y" は、x というオブジェクトに対して y というメッセージの送信である。オブジェクトに対する操作のすべてはメッセージの送信によって行われる。

表現上は似ていても背景にある考え方はずいぶん異なるようだ。オブジェクトに対してメッセージを送るということがイメージできるようになったのは RubyObjective-C を知ってからだなと思う。それまではメッセージを送るというより、オブジェクトのメソッドを呼び出すというイメージだった。Java とか C# の話。Python もそうだね。