Python and Smalltalk call models

James_Althoff at i2.com James_Althoff at i2.com
Tue May 15 17:58:06 EDT 2001


Michael writes:

:James_Althoff at i2.com writes:
:
:> 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
:
:This I agree with.
:
:> (you have to create the bound method using "new.instancemethod" in the
:> "new" module to make this work).
:
:This, however, isn't true:


You're right.  "Have to" is incorrect.  I should have said "could".  I was
thinking more of the case where you might want to create an
instance-specific method from scratch by starting with a function def.
But, of course, you can also assign a method to an instance by getting a
method from an existing instance-attribute reference as your example shows.
Thanks for pointing this out.


:>>> class C:
:...  def foo(self):
:...   print 1
:...
:>>> spam = C()
:>>> spam.eggs = spam.foo
:>>> spam.eggs()
:1
:>>> spam.__dict__['eggs']
:<method C.foo of C instance at 0x81c78e4>
:
:> 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.
:
:This is certainly true again.
:
:Cheers,
:M.


Jim





More information about the Python-list mailing list