Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between Smalltalk and python?

I'm studying Smalltalk right now. It looks very similar to python (actually, the opposite, python is very similar to Smalltalk), so I was wondering, as a python enthusiast, if it's really worth for me to study it.

Apart from message passing, what are other notable conceptual differences between Smalltalk and python which could allow me to see new programming horizons ?

like image 313
Stefano Borini Avatar asked Aug 12 '09 23:08

Stefano Borini


2 Answers

In Python, the "basic" constructs such as if/else, short-circuiting boolean operators, and loops are part of the language itself. In Smalltalk, they are all just messages. In that sense, while both Python and Smalltalk agree that "everything is an object", Smalltalk goes further in that it also asserts that "everything is a message".

[EDIT] Some examples.

Conditional statement in Smalltalk:

((x > y) and: [x > z])
  ifTrue: [ ... ]
  ifFalse: [ ... ]

Note how and: is just a message on Boolean (itself produced as a result of passing message > to x), and the second argument of and: is not a plain expression, but a block, enabling lazy (i.e. short-circuiting) evaluation. This produces another Boolean object, which also supports the message ifTrue:ifFalse:, taking two more blocks (i.e. lambdas) as arguments, and running one or the other depending on the value of the Boolean.

like image 78
Pavel Minaev Avatar answered Sep 27 '22 15:09

Pavel Minaev


As someone new to smalltalk, the two things that really strike me are the image-based system, and that reflection is everywhere. These two simple facts appear to give rise to everything else cool in the system:

  • The image means that you do everything by manipulating objects, including writing and compiling code
  • Reflection allows you to inspect the state of any object. Since classes are objects and their sources are objects, you can inspect and manipulate code
  • You have access to the current execution context, so you can have a look at the stack, and from there, compiled code and the source of that code and so on
  • The stack is an object, so you can save it away and then resume later. Bingo, continuations!

All of the above starts to come together in cool ways:

  • The browser lets you explore the source of literally everything, including the VM in Squeak
  • You can make changes that affect your live program, so there's no need to restart and navigate your way through to whatever you're working on
  • Even better, when your program throws an exception you can debug the live code. You fix the bug, update the state if it's become inconsistent and then have your program continue.
  • The browser will tell you if it thinks you've made a typo
  • It's absurdly easy to browse up and down the class hierarchy, or find out what messages a object responds to, or which code sends a given message, or which objects can receive a given message
  • You can inspect and manipulate the state of any object in the system
  • You can make any two objects literally switch places with become:, which lets you do crazy stuff like stub out any object and then lazily pull it in from elsewhere if it's sent a message.

The image system and reflection has made all of these perfectly natural and normal things for a smalltalker for about thirty years.

like image 22
Dial Z Avatar answered Sep 27 '22 15:09

Dial Z