IsPython really O-O?

Daniel Klein danielk at aracnet.com
Wed Nov 14 15:28:11 EST 2001


On Tue, 13 Nov 2001 11:59:00 -0700, "Andrew Dalke" <dalke at dalkescientific.com> wrote:

[snip]

>When I first started Python, I wrote a class like:
>
>class Spam:
>  def __init__(self, x):
>    self.x = x
>  def show(self):
>    print self.x
>
>After I figured out it worked, I wanted to add a new method, so
>I did
>
>class Spam:
>  def double(self):
>    self.x = self.x * 2
>
>then was annoyed that it didn't work the way I expected.  So I
>don't think you are alone in this view.  I bring it up here because
>I suspect for many people this is forgotten amoung all the other
>confusions in the early stages of learning Python.

Just curious to know how it is that you expected this to work and why you were 'annoyed'? I can't
see what the problem is with the double() method.

>>> class Spam:
	def __init__(self,x):
		self.x = x
	def show(self):
		print self.x
	def double(self):
		self.x = self.x * 2
		
>>> s = Spam(1)
>>> t = Spam('foo')
>>> s.double()
>>> t.double()
>>> s.x
2
>>> t.x
'foofoo'
>>> s.show()
2
>>> t.show()
foofoo 

Daniel Klein




More information about the Python-list mailing list