I am attempting to improve a Bash to Python translator.
Bash allows ++
and --
while Python does not. Many people have suggested that ++x
can be rewritten as x += 1
. But this ignores the requirement that one be able to recover the resulting value of x
before or after incrementing / decrementing as part of a potentially complex bash expression eg:
x=$(( ++y + z--)).
I can find no way of converting the above to a python expression, because it appears python does not allow side effects such as assignment within an expression. A fall back would be to write a pre/post function that took a variable or string as input, and altered its external item so passed to the function. But I can't work out how to have a function alter its input parameter variables.
If I knew that the item being operated on was always an atomic value, I might be able to alter it by mapping its string name to the dictionary to recover this value and so modify it. But ++
has to work with anything bash might throw at it: eg ++y[1]
.
Is there any way of avoiding having to take such complex bash expressions containing assignments, and break them into separate python steps that build up the overall computation performed in bash, statement by statement. This would be pretty ugly.
I just need to implement call by reference somehow in python.