views:

263

answers:

2

I dived into understanding the Ruby object model in the last weeks, and although so far was only a user of the fruits of ruby's and python's object in the past, I became curious how these things might differ in other languages.

Years ago I touched smalltalk's squeak. Smalltalk is often figuring as a referential object oriented language, that's why I am interested in it. The ruby object model does not make distinction between objects and classes, the existence singleton object makes every object a prototype extendible when necessary. As I know all these things where defined as the meta-object protocol. These things make life easier than what other less-or-non-dynamic languages like C++ or Java enable.

Please could you give me an idea, how dynamic languages, particularly smalltalk, python and ruby differ in the realisation of the meta-object protocol?

+2  A: 

The main difference between Python and Smalltalk that I remember is the way attribute privacy is handled. In Smalltalk I defined attributes and had to generate all the accessors instantly (fortunately Dolphin Smalltalk did this) and the use them. On the other hand in Python everything can be accessed, even attributes considered private (those with __ at the beginning, which are mangled to form _). Some might say, that this is potentially dangerous - say, at some point in the future you need to perform some operations, when you change certain attribute. But Python solves it gracefully, with properties.

I like the notion, that I can access anything I want. If only I know, what I am doing, I can do it :-)

gruszczy
Having all attributes of an object be public, which appears to be how Python does it, seems dangerous. This appears to make it impossible to hide the internals of an object from user code which might decide to do the wrong thing for all the right reasons.
Bob Jarvis
Python programmers don't see any reason for hiding anything from the user. They just sometimes write comments: #don't touch this ;-) If you don't know, what you are doing, and still touch them, you will be hurt. But it's like with knives - they are dangerous, but you won't slice your bread without them :-)
gruszczy
+2  A: 

In Python, each object has one namespace -- the object's "attributes", accessible in that namespace, can be methods, simple references to other objects (callable or not), or be synthesized on the fly by descriptors (plain attributes may live in the instance or in the class, but descriptors -- including functions as descriptors that synthesize methods -- are only used if they live in the class, not the instance; so in particular "special methods" are only special if defined in the class, not the instance). Attribute-handling builtins and special methods (getattr, setattr, __getattr__, __setattr__, ...) work just the same way across the single namespace of the object, whether referring to "plain attributes" or not.

The key point is that for any object a, in Python, a.b may be a method (or other callable) or not: the Python compiler doesn't know (nor care), you can take the reference a.b and (e.g.) pass it as an argument, return it as a result, append it to a list, and so forth, none of these operations implies calling a.b. If and when you want to (try and) call a.b, you do it explicitly by postfixing parentheses: a.b() if you want to call it without arguments.

In Ruby, methods and attributes of an object live in separate namespaces (so you can have an object with a method and an attribute with the same name, which in Python you can't) and "just mentioning" an argument-less method implicitly calls it (so c=a.b might be just taking an attribute reference, or it might be calling a.b(); if b names both a method and an attribute in a, I don't recall what heuristic rule is used to disambiguate the use). So if you want to just take method references (e.g to stash in some container or use as arguments or return values), and later perform the call, you use different syntax.

Smalltalk also has separate namespaces, like Ruby, but then you can't refer to a given object's "non-method" attributes (each object only "sees" its own attributes), so this ambiguity does not arise (but you still do have to use specific messages to extract, and later call, a "method reference").

Alex Martelli
In Ruby, `a.b` *always* means a message send (i.e. it is *always* equivalent to `a.b()`), and *never* an attribute reference, simply because there is no such thing as an "attribute" in Ruby. There are only instance variables, but those are (as the name already implies) private to the instance and can *never* be accessed from the outside. (Let's ignore reflection, once you do that, all bets are off.)
Jörg W Mittag
I don't see how this can be the 'accepted answer' when 50% of the commentary on Ruby is flat-out incorrect, as pointed out by Jorg.
banister