views:

763

answers:

6

I've seen some comparisons between Smalltalk and Ruby on the one hand and Ruby and Python on the other, but not between Python and Smalltalk. I'd especially like to know what the fundamental differences in Implementation, Syntax, Extensiabillity and Philosophy are.

For example Python does not seem to have Metaclasses. Smalltalk has no concept of generators. And although both are said to be dynamicly typed, I believe that Python does not do dynamic method dispatch. Is this correct?

+1  A: 

According to Wikipedia's page on dynamic method dispatch:

Smalltalk Implementation

Smalltalk uses a type based message dispatcher. Each instance has a single type whose definition contains the methods. When an instance receives a message, the dispatcher looks up the corresponding method in the message-to-method map for the type and then invokes the method. [...]

Many other dynamically typed languages, including Python, Ruby, Objective-C and Groovy use similar approaches.

Emphasis added, and one paragraph snipped. So, at least that part seems to be similar between the two languages.

unwind
+1  A: 

I've been reading coders at work which is a really nice book full of interviews with top programmers. Anyhow, one of them is the inventor of smalltalk and he talks in length on his language and how it relates to python (he likes python quite a bit as well). The only problem he had with python was it's slow code... he really wanted to have the smalltalk jit compiler as a backend for python, but unfortunately due to the software belonging to the company he worked for, this was not possible.

anyhow... maybe not a point by point comparison, but really a good read anyway this book.

Toad
+6  A: 

Python certainly does have metaclasses.

Smalltalk has some unusual features:

  • Has a rather simple syntax and only about 6 (!) keywords. Everything else (including defining new classes) is accomplished by calling methods (sending messages in Smalltalk). This allows you to create some DSL within the language.
  • In Smalltalk, you don't store source files, but instead have one big memory image and you modify it on the fly. You can also modify most of the Smalltalk itself (and possibly break it ;)
Roman Plášil
+7  A: 

For example Python does not seem to have Metaclasses.

It sure does -- it just doesn't implicitly generate a new metaclass for every class: it uses the same metaclass as the parent class, or type by default. Python's design philosophy, aka "The Zen of Python", can be perused by doing import this at an interactive interpreter's prompt; the applicable point here is the second one, "Explicit is better than implicit."

In Python 2.X, you specify a custom metaclass with the following syntax:

class sic:
  __metaclass__ = mymeta
  ...

In Python 3.X, more elegantly, you use named-argument syntax:

class sify(metaclass=mymeta):
  ...

Smalltalk has no concept of generators.

Python's generators are first-class (typically standalone) functions, and Smalltalk doesn't have the concept of "standalone" functions -- it has methods inside classes. But it certainly does have iterators -- as classes, of course:

iterator := aCollection iterator.
[iterator hasNext] whileTrue: [iterator next doSomething].

Since Smalltalk has first-class "code blocks" (Ruby took them from it), you accomplish iteration, just like other "control structures", by sending a code block to a suitable method, and if you wish you can do that directly with the collection (think select:):

aCollection select: [:item | item doSomething].

So in Smalltalk (and Ruby) you send the code block to the iteration; Python does things the other way round, the iteration sends values out to the surrounding "calling" code. Looks very different, but not "deeply" different in the end.

First-class code blocks mean that Smalltalk doesn't need nor have "control structure" statements and keywords such as if or while: they can be accomplished by sending code blocks as arguments of appropriate methods (e.g. ifTrue: method of booleans). (Ruby chooses to have the keywords/statements in addition to the first-class code blocks; I would say that Python [[explicitly]] and Smalltalk [[implicitly]] both try, like C, to "offer a single way to perform an operation", while Ruby's more in the Perl-ish school of "there are many ways to do it").

And although both are said to be dynamicly typed, I believe that Python does not do dynamic method dispatch. Is this correct?

No, absolutely incorrect -- Python intensely does dynamic method dispatch, to extremes. Consider for example:

for i in range(10):
  myobject.bah()

By Python semantics, this performs 10 lookups for method bah in myobject -- just in case the previous execution of the method had caused myobject to entirely restructure itself internally so that its current bah method is completely different from the previous one (might be a pretty insane thing for the programmer to rely on such furious dynamism, but Python supports it). This is the reason that makes:

themethod = myobject.bah
for i in range(10):
  themethod()

a common hand-optimization in Python code -- does one dynamic lookup before the loop instead of 10 inside the loop, one per leg (it's a case of "constant hoisting", since the compiler is forbidden from doing the "constant folding" itself by Python's extreme rules for dynamic lookups -- unless it can prove that it's guaranteed to be innocuous, and in practice such proof is too hard so Python implementations typically don't bother).

Python uses unified namespaces: methods are attributes of an object just like any other, except that they're callable. This is why extracting the method without calling it (known as a "bound method"), setting a reference to it in a variable (or stashing it into a list or other container, returning it from a function, whatever) is a plain and simple operation like in the above constant-hoisting example.

Smalltalk and Ruby have separate namespaces for methods and other attributes (in Smalltalk, non-methods attributes are not visible outside the object's own methods), so "extracting a method" and "calling the resulting object" require more introspective ceremony (but the common case of dispatching may be thereby made marginally simpler in certain cases -- in particular, "just mentioning" an argument-less method implicitly calls it, while in Python, like in C, calling is explicitly performed by appending parentheses, while "just mentioning", well... "just mentions" it, making it available for any sort of explicit operation including calling;-).

Alex Martelli
A: 

With regard to the last post by reinier, I just read coders at work, the interview was with don ingalls, who was the implementer of smalltalk, rather than the inventor. The inventor is Alan Kay.

forioros
Dan Ingalls is his name
wbg
+1  A: 

Smalltalk has no concept of generators.

True, but they can be implemented in most Smalltalk dialects from within the language. GNU Smalltalk comes with Generators as part of its stream library.

Lukas Renggli