Initializing with the correct type

aine_canby at yahoo.com aine_canby at yahoo.com
Thu Dec 7 07:56:03 EST 2006


Hi all,

I'm new to Python and I'm just wordering if my approch is correct.
Here's an example. I'm making sure that the length and types are
correct. This is in case I use such a class and accidently pass it the
wrong object.

class Funkt:
	'Funkt Class'

	def __init__(self, L):
		'Constructer, accepts a list L of ints, which is 1 or listLength in
length'
		if len(L) not in (1,listLength):
			errorString = "Format Error: L must be 1"
			if listLength != 1:
				errorString += " or "+str(listLength)
			errorString += " in Length"
			raise FormatError,errorString
		for i in L:
			if type(i) is not int:
				raise FormatError, "L must contain ints"


class FunktError(Exception):
	"Exceptions base class for FUnkt class"
	pass

class FormatError(FunktError):
	"Exception raised for wrong list length."

	def __init__(self, message):
	        self.message = message

	def __str__(self):
        	return self.message




More information about the Python-list mailing list