[Tutor] newbie-class differs from function?

Lugo Teehalt lugoteehalt@yahoo.co.uk
Fri Mar 14 11:27:02 2003


--0-90400980-1047659208=:16079
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit


 

I put in origional question and have been greatly helped. So these

things really work, surprisingly perhaps.

Putting in print statements helped, very simply, to work out what the computer

does with a class

>>> class Counter:

def __init__(self):

print "__init__ method called"

self.reset()

def incr(self):

print "incr ?method called"

self.counter += 1 

def reset(self): 

print "reset called"

self.counter = 0

def __str__(self):

print "__str__ method called"

return str(self.counter)



>>> instance1 = Counter()

__init__ method called

reset called

>>> instance2 = Counter()

__init__ method called

reset called

>>> instance1.incr()

incr ?method called

>>> instance2.incr()

incr ?method called

>>> instance2.incr()

incr ?method called

>>> print instance1, instance2

__str__ method called

1 __str__ method called

2

>>> instance1

<__main__.Counter instance at 0x82e74ac>

>>> instance1.reset()

reset called

>>> str(instance1)

__str__ method called

'0'

even roughly understand inheritance:



>>> class addcounters(Counter):

def __add__(self, other=0):

print "addcounters' __add__ method called"

return (string.atoi(str(self)) + string.atoi(str(other)),"__add")



>>> instnc1 = addcounters()

__init__ method called

reset called

>>> instnc2 = addcounters()

__init__ method called

reset called

>>> # addcounters() has inherited the incr ?method:

>>> instnc1.incr()

incr ?method called

>>> instance1 + instance2

Traceback (most recent call last):

File "<pyshell#27>", line 1, in ?

instance1 + instance2

TypeError: unsupported operand types for +: 'instance' and 'instance'

>>> instnc1 + instnc2

addcounters' __add__ method called

__str__ method called

__str__ method called

(1, '__add')

>>> instnc1 - instnc2

Traceback (most recent call last):

File "<pyshell#29>", line 1, in ?

instnc1 - instnc2

TypeError: unsupported operand type(s) for -: 'instance' and 'instance'

>>> 

All of this stuff is news to me.

 

 




---------------------------------
With Yahoo! Mail you can get a bigger mailbox -- choose a size that fits your needs

--0-90400980-1047659208=:16079
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

<P>&nbsp;</P><FONT size=2>
<P>I put in origional question and have been greatly helped. So these</P>
<P>things really work, surprisingly perhaps.</P>
<P>Putting in print statements helped, very simply, to work out what the computer</P>
<P>does with a class</P>
<P>&gt;&gt;&gt; class Counter:</P>
<P>def __init__(self):</P>
<P>print "__init__ method called"</P>
<P>self.reset()</P>
<P>def incr(self):</P>
<P>print "incr ?method called"</P>
<P>self.counter += 1 </P>
<P>def reset(self): </P>
<P>print "reset called"</P>
<P>self.counter = 0</P>
<P>def __str__(self):</P>
<P>print "__str__ method called"</P>
<P>return str(self.counter)</P>
<P></P>
<P>&gt;&gt;&gt; instance1 = Counter()</P>
<P>__init__ method called</P>
<P>reset called</P>
<P>&gt;&gt;&gt; instance2 = Counter()</P>
<P>__init__ method called</P>
<P>reset called</P>
<P>&gt;&gt;&gt; instance1.incr()</P>
<P>incr ?method called</P>
<P>&gt;&gt;&gt; instance2.incr()</P>
<P>incr ?method called</P>
<P>&gt;&gt;&gt; instance2.incr()</P>
<P>incr ?method called</P>
<P>&gt;&gt;&gt; print instance1, instance2</P>
<P>__str__ method called</P>
<P>1 __str__ method called</P>
<P>2</P>
<P>&gt;&gt;&gt; instance1</P>
<P>&lt;__main__.Counter instance at 0x82e74ac&gt;</P>
<P>&gt;&gt;&gt; instance1.reset()</P>
<P>reset called</P>
<P>&gt;&gt;&gt; str(instance1)</P>
<P>__str__ method called</P>
<P>'0'</P>
<P>even roughly understand inheritance:</P>
<P></P>
<P>&gt;&gt;&gt; class addcounters(Counter):</P>
<P>def __add__(self, other=0):</P>
<P>print "addcounters' __add__ method called"</P>
<P>return (string.atoi(str(self)) + string.atoi(str(other)),"__add")</P>
<P></P>
<P>&gt;&gt;&gt; instnc1 = addcounters()</P>
<P>__init__ method called</P>
<P>reset called</P>
<P>&gt;&gt;&gt; instnc2 = addcounters()</P>
<P>__init__ method called</P>
<P>reset called</P>
<P>&gt;&gt;&gt; # addcounters() has inherited the incr ?method:</P>
<P>&gt;&gt;&gt; instnc1.incr()</P>
<P>incr ?method called</P>
<P>&gt;&gt;&gt; instance1 + instance2</P>
<P>Traceback (most recent call last):</P>
<P>File "&lt;pyshell#27&gt;", line 1, in ?</P>
<P>instance1 + instance2</P>
<P>TypeError: unsupported operand types for +: 'instance' and 'instance'</P>
<P>&gt;&gt;&gt; instnc1 + instnc2</P>
<P>addcounters' __add__ method called</P>
<P>__str__ method called</P>
<P>__str__ method called</P>
<P>(1, '__add')</P>
<P>&gt;&gt;&gt; instnc1 - instnc2</P>
<P>Traceback (most recent call last):</P>
<P>File "&lt;pyshell#29&gt;", line 1, in ?</P>
<P>instnc1 - instnc2</P>
<P>TypeError: unsupported operand type(s) for -: 'instance' and 'instance'</P>
<P>&gt;&gt;&gt; </P>
<P>All of this stuff is news to me.</P>
<P>&nbsp;</P>
<P>&nbsp;</P></FONT><p><p><br><hr size=1><a href="http://uk.yahoo.com/mail/tagline_xtra/?http://uk.docs.yahoo.com/mail_storage.html"><b><font face="Arial" size="2">With Yahoo! Mail you can get a bigger mailbox -- choose a size that fits your needs</font></b></a><br>
--0-90400980-1047659208=:16079--