I have a piece of code that calculates the sum of a number of variables. For example, with 3 variables
(A = 1
, B = 2
, C = 3
) it outputs the sum X = 6
. The way the code is implemented this is set up as a list with two strings:
Y = [['X', 'A+B+C']]
The list is compiled to create a sum which is then entered in a dictionary and used by the rest of the code:
YSUM = {}
for a in Y:
YSUM[a[0]] = compile(a[1],'<string>','eval')
The code works fine, but there are instances in which there are no variables to sum and therefore the related string in the list is empty: Y = [['X', '']]
. In this case, the output of the sum should be zero or null. But I can't find a way to do it. The compile
function complains about an empty string (SyntaxError: unexpected EOF while parsing
), but doesn't seem it can accept an alternative (compile() arg 1 must be a string, bytes or AST object
).