开发者

What are the major differences in object models of dynamic languages like Smalltalk, Ruby and Python

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 开发者_JAVA技巧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?


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").


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 :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜