[Python-Dev] Generating nested data structures with blocks

Walter Dörwald walter at livinglogic.de
Mon May 2 18:06:58 CEST 2005


Reading PEP 340, it seems to me that blocks could be used for generating 
nested data structures:

def blist(list):
	def enter(parent=None):
		if parent:
			parent.append(self)
		yield self

x = blist()
block x.enter() as x:
	x.append(1)
	block blist().enter(x) as x:
		x.append(2)
	x.append(3)

print x

this should print [1, [2], 3]

For this to work, the scope of the block variable has to end with the 
end of the block. Currently the PEP leaves this unspecified.

Bye,
    Walter Dörwald


More information about the Python-Dev mailing list