A dumb question about a class

Dick Moores rdm at rcblue.com
Sun Aug 12 23:49:11 CEST 2007


I'm still trying to understand classes. I've made some progress, I 
think, but I don't understand how to use this one. How do I call it, 
or any of its functions? It's from the Cookbook, at 
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/523048>.

Thanks,

Dick Moores

=====================================================
class PrimeList:
	def __init__(self, initial=0):
		self.primelist = [2,3]
		self.primelookup = [0,0,1,1]
		self.max_prime = 3
		self.grow_primelist(initial)

	def grow_primelist(self,number):
		newprimes = []
		while self.max_prime <= number:
			next = self.nextprime()
			newprimes.append(next)
			self.max_prime = next
			size_difference = self.max_prime - len(self.primelookup) + 1
			self.primelookup.extend([0] * size_difference)
		for i in newprimes:
			self.primelookup[i]=1
	
	def contains(self,number):
		if number < 2:
			return 0
		if number > len(self.primelookup) - 1:
			self.grow_primelist(number)
			return self.primelookup[number]
		return self.primelookup[number]
	
	def nextprime(self):
		i = self.max_prime + 2
		while 1:
			isprime = True
			for prime in self.primelist:
				if i % prime == 0:
					isprime = False
					i += 2
					break
			if isprime:
				self.primelist.append(i)
				return(i)
======================================================




More information about the Python-list mailing list