the deceptive continuous assignments

Terry Reedy tjreedy at udel.edu
Tue Dec 6 17:11:45 EST 2011


On 12/6/2011 7:33 AM, Chris Angelico wrote:
> On Tue, Dec 6, 2011 at 11:20 PM, Terry Reedy<tjreedy at udel.edu>  wrote:
>> You found an unsafe overlap.
>> x.thing = x = 1
>> would work, though it seems strange (and unlikely in practice) to rebind x
>> to an int after it is bound to a class k instance.
>
> This code is starting to look like it wants to work with a linked list.
>
>>>> class node:
> 	def __init__(self,x):
> 		self.payload=x
> 		self.next=None
> 	def walk(self):
> 		print("My payload is: "+self.payload)
> 		if self.next: self.next.walk()
>
>>>> head=tail=node("This")
>>>> tail.next=tail=node("is")
>>>> tail.next=tail=node("a")
>>>> tail.next=tail=node("test.")
>>>> head.walk()
> My payload is: This
> My payload is: is
> My payload is: a
> My payload is: test.

Cute. I am used to linked lists being built from the bottem up in 
functional languages with immutable nodes. I might even use something 
like this. Of course, for a list of any length, walk needs to be iterative.

     def walk(self):
         while self:
             print("My payload is: "+self.payload)
             self = self.next

-- 
Terry Jan Reedy




More information about the Python-list mailing list