Python and Smalltalk call models (was Re: Why aren't we all speaking LISP now?)

James_Althoff at i2.com James_Althoff at i2.com
Mon May 14 18:16:09 EDT 2001


Fredrik writes:
>what exactly is the difference between Python's call model
>and Smalltalk's message passing?  consider something like:
>
>    spam.egg(bacon)
>
>couldn't this be seen as sending the message "egg" to the
>object "spam"?
>
>Cheers /F

Pretty much the same.  Two areas of difference:

1) In Python, "egg" could be an instance attribute that references a bound
method associated with the *instance* "spam" (i.e., that lives in the dict
of "spam") -- although I believe this occurs not so frequently in practice
(you have to create the bound method using "new.instancemethod" in the
"new" module to make this work).  Smalltalk would always look in the method
dict of the class of "spam" to find the method "egg" -- IOW, Smalltalk
would not try to find an instance-specific method first.

2) If "spam" is a *class object*, then in Python "spam.egg(bacon)" would
mean the same as "bacon.egg()" -- assuming that "bacon" is an instance of
class "spam".  In Smalltalk, it would mean "look in the method dict of the
*class* of class object 'spam' to find the method 'egg'; then invoke method
'egg' on class object 'spam' passing in 'bacon' as an arg."  In Smalltalk
class objects are also instances -- of metaclasses -- so the message/method
lookup mechanism is the same for both "class methods" and "instance
methods".

Both of the above -- especially the second -- contribute to making "class
methods" somewhat problematic in current Python.

Jim






More information about the Python-list mailing list