1

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 ...

sten
  • 7,028
  • 9
  • 41
  • 63
  • 1
    A fancy use of `@property` inside a custom class sounds like it could work. I can't think of a graceful way of doing this though. – SyntaxVoid Dec 10 '19 at 21:26

1 Answers1

2

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • it is obvious ;) .. thinking if this solves my problem ... have to do some experiments .. – sten Dec 10 '19 at 22:25