IsPython really O-O?

Blake Winton bwinton at tor.dhs.org
Sat Nov 17 21:42:54 EST 2001


On Wed, 14 Nov 2001 16:05:21 -0700, Andrew Dalke wrote:
>Me:
>>>I wanted to add a new method, so I did
>>>
>>>class Spam:
>>>  def double(self):
>>>    self.x = self.x * 2
>
>Daniel Klein:
>>Just curious to know how it is that you expected this to work
>
>Because I didn't think that 'class Spam' always created a new
>class.  I thought it would reuse a definition of one already exists.
>I believe this is similar to what Smalltalk does.
>
>> why you were 'annoyed'?
>
>Because it didn't fit my model of how the language worked, so
>was more effort to learn.
>
>>I can't
>>see what the problem is with the double() method.
>
>The problem was I wanted to add a method to an existing class.
>The method definition was fine.

It's a bit of a pain, but...
Python 2.2a3 (#1, Sep 16 2001, 12:18:17) 
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Spam:
...   def __init__( self ):
...     self.x = 3
...       
>>> spam1 = Spam()
>>> dir( spam1 )
['__doc__', '__init__', '__module__', 'x']
>>> dir( Spam )
['__doc__', '__init__', '__module__']
>>> def newMeth( self ):  
...   self.x = self.x + 1
... 
>>> Spam.__dict__["eggs"] = newMeth
>>> dir( Spam )
['__doc__', '__init__', '__module__', 'eggs']
>>> dir ( spam1 )
['__doc__', '__init__', '__module__', 'eggs', 'x']
>>> spam1.eggs
<bound method Spam.newMeth of <__main__.Spam instance at 0x8139094>>
>>> spam1.eggs()
>>> spam1.x
4
>>> 

So, it seems to me that all you really want is syntactic sugar for
that...  I guess since it's not really in Python's community
standards to add methods to classes (aside from the Twisted folk),
it's not horrible that it's a little tricky...

Later,
Blake.
-- 
9:40pm up 52 days, 21:07, 2 users, load average: 0.02, 0.09, 0.07



More information about the Python-list mailing list