statements in control structures (Re: Conditional Expressions don't solve the problem)

Rich Harkins rharkins at
Thu Oct 18 21:53:01 CEST 2001

Neither of these will do the trick nicely because in order to actually
modify the calling context one must work back through the stack backtraces.
For example, let's use the let function proposed:

def let(l,v):
	return l[0]

print a

This prints 5 because let only modifies the first element of l and since the
variable "a" does not reside in the arguments (only its referenced object
does) the list gets changed but "a" goes merrily on.  You could say:

def let(d,name,v):
	return v


... but that is pretty ugly not to mention hacky.  Another hacky form of
this is:

import sys
def let(name,v):
		raise Exception
		del t,v,tb
	return v

print a

That works but it too is extremely ugly, unlikely to work in all conditions,
and would perform poorly.  So unless Python will almost certainly have to
grow a let extension if this effect is desired.


> -----Original Message-----
> From: python-list-admin at
> [mailto:python-list-admin at]On Behalf Of Christopher A. Craig
> Sent: Thursday, October 18, 2001 10:23 AM
> To: Anders J. Munch
> Cc: python-list at
> Subject: Re: statements in control structures (Re: Conditional
> Expressions don't solve the problem)
> "Anders J. Munch" <andersjm at> writes:
> > Here's an additional alternative:
> >
> > Use a function-like form let to perform assignment within expression.
> >   let(X,Y)
> > has the effect of doing
> >   X = Y
> > and returning the assigned object.
> I had thought about doing this earlier.  You could do this now
> with a little
> ugly syntax:
> def let(a, b):
>   a[0] = b
>   return a[0]
> while let([a], get_next()):
>   process(x)
> The problem being that you have to pass the variable as an element in a
> mutable structure in order to modify it.  The problem with your proposal
> being that you don't (and it would be the only function where you don't).
> --
> Christopher A. Craig <com-nospam at>
> --

More information about the Python-list mailing list