sten
sten

Reputation: 7476

late code evaluation with late variable binding?

You can postpone code execution :

In [12]: v = 5

In [13]: e = ' v * 2 '

In [14]: eval(e)
Out[14]: 10

I would like to do late evaluation of normal python code w/o assigning it to a string ?

Is there a technique to do this ? closures ? __call__ ?


another example :

In [15]: b = bitarray('10110')

In [16]: p = Pipe(lambda x : x * 2 )

In [17]: e = ' b | p '

In [18]: eval(e)
Out[18]: bitarray('1011010110')

i'm trying to build something like diagram/pipe of execution flow similar TensorFlow & keras and then pass the data and collect the result... it is abit more complicated than that because the flow is not straght-forward ...

Upvotes: 1

Views: 189

Answers (1)

Mark Ransom
Mark Ransom

Reputation: 308206

The usual way to do this is with a function.

def e():
    return v * 2

>>> v = 5
>>> e()
10
>>> v = 6
>>> e()
12

I must also say that I'm not in favor of functions that don't take their input as explicit parameters. Grabbing a global is cheating.

Upvotes: 2

Related Questions