A Suggestion for Python Dictionary/Class

Jay O'Connor joconnor at cybermesa.com
Fri Dec 22 19:41:12 EST 2000


> Therefore, is there any way for me to define a class, with some definite
> members just like in C/C++/Java, so that no new members can be defined? 


You might want to define all your instance variables in __init__() and
the overload __setattr__ to not allow anyone to set a value unless
already defined

class MyClass:
	def __init__(self):
		self.member = 0

	def __setattr__(self, name, value):
		if self.__dict__.has_key(name):
			self.__dict__[name]=value
		else:
			raise: "Error - no instance variable named "+ name

This will flag a runtime error if you try

o = MyClass()
o.menber = 1


Take care,

-- 
Jay O'Connor
joconnor at cybermesa.com
http://www.cybermesa.com/~joconnor

"God himself plays the bass strings first when He tunes the soul"




More information about the Python-list mailing list