0

Say you have a function

def func():
    print(1)
    print(2)
    print(3)

Does there exist some functionality in python which provides the "reverse" of this function. The reverse would print 3, print 2, and then print 1.

For longer functions (specifically, inverses in symmetric key encryption) this would be a very handy tool.

Edit: The function only consists of many independent statements as well as iteration and branching

If my function was the following

def a():
    b()
    for i in range(1,3):
        c()
        d()

Then the reverse would call: d,c,d,c,b. The interpreter would loop over the range iterator (which is [1,2]) backwards giving [2,1]. The reverse functionality would make subcalls on any iteration blocks

tyron
  • 9
  • 3
  • 3
    No. Because in a general case it won't even make any sense to reverse the code. – Eugene Sh. Oct 19 '21 at 13:46
  • 1
    For functions that _return_ a list of values, you can reverse that list. You can't unprint what was printed by the function. (Except for some serious print-redirecting decorator magic) – tobias_k Oct 19 '21 at 13:46
  • Sure it will if you talk about assembly level code. If you define a block at the locations 10, 20, 30, then simply run it backwards by calling 30, 20, 10 – tyron Oct 19 '21 at 13:47
  • Rather than making the function itself print things, have it *return* a sequence of lines that the *caller* could print. Then it's a simple matter of just reversing the sequence before printing. – chepner Oct 19 '21 at 13:47
  • 2
    @tyron That makes heavy assumptions about instruction dependencies. – chepner Oct 19 '21 at 13:48
  • 1
    In particular, that there is *no* dependency of each instruction on the any of the previous ones. Which is of course false for virtually any non-trivial code. – Eugene Sh. Oct 19 '21 at 13:51
  • Sure, but the code I am concerned with is a list of independent statements. It would not reverse the functions past the initial function call – tyron Oct 19 '21 at 13:54
  • @tyron There is no point to introduce some special functionality in the language to satisfy such a non-realistic use-case. – Eugene Sh. Oct 19 '21 at 13:55
  • Do you know how it would be implemented? Perhaps in C or lower level languages – tyron Oct 19 '21 at 13:55
  • It can be implemented in a *higher* level language. For example you can write your own interpreter that will take a list of instructions and process it in any direction you want. You can even design this language to be generally "reversible", but this will introduce certain (heavy) constraints on it. FWIW, quantum algorithms are reversible. – Eugene Sh. Oct 19 '21 at 13:57
  • Okay, a list of statements can be executed in reverse quite easily I see that now. But what about arguments, conditional loops, etc. – tyron Oct 19 '21 at 14:01
  • @tyron Once again - it is *not possible*. Imperative language code is not reversible *in general*. – Eugene Sh. Oct 19 '21 at 14:02
  • Why exactly is it not possible, I would like to know – tyron Oct 19 '21 at 14:03
  • Because a simple sequence such as `a=1; a=a+1` does not make sense if reversed. – Eugene Sh. Oct 19 '21 at 14:04
  • I understand that but I am referring to the case of independent statements with included functionality for iterative loops and branching. In the case of a loop block, it would go to the start of the block to initialise a variable and then back to the end of the block – tyron Oct 19 '21 at 14:06
  • OK, then simply take it as a fact - there is no such a functionality in any of the imperative language I know of. Nor in functional or logical ones. Because this would be largely useless as there is no practical code that would make sense if reversed. – Eugene Sh. Oct 19 '21 at 14:13
  • If you reversed a function that includes branching (i.e. an `if`), how do you know which branch to start evaluating from the bottom up? – Samwise Oct 19 '21 at 14:14
  • I would like to restate my question: how would you construct a recursive function which does this? It would have to be recursive because of subcalls on iteration blocks – tyron Oct 19 '21 at 14:14
  • @Samwise branching goes in the normal direction, but the blocks inside the branch go backwards – tyron Oct 19 '21 at 14:15
  • Once you reach a branch block working backwards, you go to the top of it and branch normally – tyron Oct 19 '21 at 14:15
  • 1
    `foo = 3`; `foo += 2`; `if foo < 4: print("dead")`; `else: print("alive")`. What does this code print when reversed? In what order are the statements evaluated? – Samwise Oct 19 '21 at 14:22

1 Answers1

1

The easiest way to do what you're trting to do would be to save your data to a list and then using the reverse function.

nums= ['1', '2', '3']
nums.reverse()
Rodrigo
  • 181
  • 11