[Tutor] class trouble

Andrei Kulakov ak@silmarill.org
Fri, 19 Jul 2002 23:05:26 -0400


On Fri, Jul 19, 2002 at 08:08:42PM -0700, Johannes Graumann wrote:
> Hello,
> 
> I'm just starting this and I'm absolutely clueless why the 3. and 4.
> print statements give me the errors they do. Please help!
> 
> Thank you, Johannes
> 
> 
> import string
> class porf:
> 	def __init__(self,name,sequence):
> 		self.name = name
> 		self.sequence = string.upper(sequence)
> 	def length(self):
> 		return len(self.sequence)
> 	def stats(self,aa):
> 		aa = string.upper(aa)
> 		return string.count(self.sequence,aa)
> ypr123a = porf('ypr123a','advboaisugvdfklabdfvlysgdvlajhsbfdlasgdclabhcd')
> print ypr123a.name
> print ypr123a.sequence
> print ypr123a.length
> print ypr123a.stats('a')

ypr123a
ADVBOAISUGVDFKLABDFVLYSGDVLAJHSBFDLASGDCLABHCD
<method porf.length of porf instance at 0x810775c>
6

Instead of length you probably want __len__(self) - then you can do
len(instance) and it'll run that function. And to run the length
function you have, do print ypr123a.length(). 

The 4th statement is correct, I think.. Isn't it?

 - Andrei
-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org