Re: Re: Insight: Prothon isan Acquisition-orientedlanguage

Paul Prescod <[email protected]> Thu, 29 Jul 2004 18:27:26 -0700
Newsgroups gmane.comp.lang.prothon.user
Message-ID <[email protected]>
Here is a Python implementation of the style inheritance thing:

inherited_properties = ["a", "b", "c"]

class Node:
	vals = None

	def __init__(self, parent = None):
		# have to get around setattr below
		self.__dict__["vals"] = {}
		self.__dict__["parent"] = parent

	def __getattr__(self, name):
		if name in inherited_properties:
			if name in self.vals:
				return self.vals[name]
			else:
				return getattr(self.parent,name)
		else:
			return self.vals[name]


	def __setattr__(self, name, val):
		self.vals[name] = val


root = Node()

child1 = Node(root)
child2 = Node(root)
grandchild = Node(child2)
child3 = Node(root)


root.a = "foo"
root.z = "zfoo"
child3.a = "bar"

assert child2.a == root.a
assert child3.a != root.a
assert grandchild.a == root.a
assert root.a
try:
	print child1.z
	raise "Should not have got here!"
except KeyError:
	pass