Re: Is delayed evaluation possible ?

Guido van Rossum (Guido.van.Rossum@cwi.nl)
Sun, 12 Jul 1992 12:53:54 +0200

brannon@jove.cs.caltech.edu (Terrence M. Brannon) writes:

>Is it possible to alias a string that will have variables substituted
>into it if the variables are not already defined such as:
>
>xdr_stream.append('xdr_' dataType + \
> '(&(M_RemoteProcessPointer->xdr_write), \
> + rpc_arg + '); \n)
>
>can i do substr = ('xdr_' dataType + \
> '(&(M_RemoteProcessPointer->xdr_write), \
> + rpc_arg + '); \n)
>
>xdr.stream.append(substr) so that I dont have to ugly up my code ?
>
>This also has another question built into it as to whether or not
>unbound variables in strings are possible through the use of delayed
>evaluation. This is a nice feature that Tcl has.

No, there is no delayed evaluation of strings in Python, but you can
do what you want using a function (which is of course the more general
way of expressing delayed evaluation of anything):

def substr(dataType, rpc_arg):
return 'xdr_' + dataType + \
'(&(M_RemoteProcessPointer->xdr_write), ' \
+ rpc_arg + '); \n'

(note that I have supplied some missing quotes and operators in your
example :-) and then write

xdr.stream.append(substr(dataType, rpc_arg)).

You may be able to shorten your code even more if you change the
function arguments into global variables -- personally I don't like
the use of globals when arguments could be used, and Python reflects
this view by forcing you to declare your globals as such in every
function that assigns to them.

--Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
"Exploding is a perfectly normal medical phenomenon."