How does Smalltalk (like Pharo) compare with Python?

I have seen some comparisons between Smalltalk and Ruby , on the one hand, and Ruby and Python , but not between Python and Smalltalk . I would especially like to know what are the fundamental differences in implementation, syntax, extension, and philosophy.

For example, Python has no metaclasses. Smalltalk has no concept of generators. Although both of them say that they are dynamically typed, I believe that Python does not dynamically dispatch methods. Is it correct?

+6
python comparison smalltalk language-features language-comparisons
source share
5 answers

For example, Python does not seem to have metaclasses.

Of course, this just does not mean creating a new metaclass for each class: it uses the same metaclass by default as the parent class, or type . The Python design philosophy, also known as “Zen of Python,” can be examined with import this at the prompt of the interactive interpreter; the applicable point here is second: "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 the named-argument syntax:

 class sify(metaclass=mymeta): ... 

Smalltalk does not have the concept of generators.

Python generators are first-class (usually autonomous) functions, and Smalltalk does not have the concept of "autonomous" functions - it has methods inside classes. But it certainly has iterators - like 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 iterate along with the other “control structures” by sending the code block to the appropriate method, and if you want, you can do that directly with the collection (think select: ):

 aCollection select: [:item | item doSomething]. 

So, in Smalltalk (and Ruby) you send a block of code to iterate; Python does the opposite, iterating sends values ​​to the surrounding "calling" code. It looks very different, but not "deeply" different at the end.

First-class code blocks mean that Smalltalk is not needed, and also does not have instructions and keywords of the "control structure", such as if or while : they can be executed by sending code blocks as arguments to the corresponding methods (for example, ifTrue: the Boolean method) . (Ruby selects keywords / operators in addition to first-class blocks of code, I would say that Python [[explicitly]] and Smalltalk [[implicitly]] both try, like C, to “suggest one way to execute the operation”, while Ruby more at Perl-ish school "There are many ways to do this").

And although both of them say that they are dynamically typed, I that Python does not do a dynamic method of sending. It is right?

No, it’s absolutely wrong - Python intensively implements a dynamic submit method, to the extreme values . Consider, for example:

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

In Python semantics, it performs 10 searches for the bah method in myobject - in case the previous execution of the method caused myobject completely restructure itself inside, so its current bah method bah completely different from the previous one (maybe it's a very crazy thing for a programmer to rely on such furious dynamism, but Python supports it). Exactly because of this reason:

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

general manual optimization in Python code - does one dynamic search before the loop instead of 10 inside the loop, one for each leg (this is a case of "constant lifting", since the compiler is not allowed to perform "constant folding" according to the extreme Python rules for dynamic search - if only this will not prove that it is guaranteed to be harmless, and in practice such a proof is too complicated, so Python implementations are usually not bothered).

Python uses unified namespaces: methods are attributes of an object, like any other, except that they are called. This is why retrieving a method without calling it (known as the “associated method”), setting a reference to it in a variable (or copying it to a list or other container, returning it from some function), is simple and simple control, as in the above example with a constant climb.

Smalltalk and Ruby have separate namespaces for methods and other attributes (in Smalltalk, non-method attributes are not visible outside the object’s own methods), so “method extraction” and “calling the resulting object” require more introspective ceremony (but in some cases the general dispatch case can be simplified a little - in particular, the “just mentioning” method without arguments invokes it implicitly, whereas in Python, as in C, the call is explicitly performed by adding parentheses, while “just mentioning”, well ... " just mentions "him, elaya it available to any explicit operations, including call; -).

+9
source share

Python has metaclasses.

Smalltalk has some unusual features:

  • It has a fairly simple syntax and only about 6 (!) Keywords. Everything else (including defining new classes) is done by calling methods (sending messages to Smalltalk). This allows you to create some DSL within the language.
  • In Smalltalk, you do not store the source files, but instead have one large image of memory, and you modify it on the fly. You can also change most of Smalltalk itself (and possibly break it;)
+7
source share

I read coders at work , which is a really good book, full of interviews with top programmers. In any case, one of them is the inventor of smalltalk, and he talks in detail about his language and how he relates to python (he also likes python). The only problem he ran into python was slow code ... he really wanted to have the smalltalk jit compiler as the backend for python, but unfortunately because of the software owned by the company he worked for, it was impossible.

anyway ... maybe not a step-by-step comparison, but it's actually good to read this book anyway.

+2
source share

Smalltalk has no idea about generators.

True, but they can be implemented on most Smalltalk dialects within the language. GNU Smalltalk comes with generators as part of the stream library .

+2
source share

According to the Wikipedia page of the dynamic submit method:

Smalltalk Implementation

Smalltalk uses a dispatcher type message. Each instance has one type, the definition of which contains methods. When an instance receives a message, the dispatcher scans the corresponding method in the message map for the method for the type and then calls the method. [...]

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

Emphasis is added and one paragraph is cut off. Thus, at least this part seems similar between the two languages.

+1
source share

All Articles