Checking if the value of a string is true - python
<p>I am trying to see if a string is true or false but im stuck... In the following code I have made a list of statements that I want to evaluate, I have put them in strings because the variables need to be declared after the list is .(Reason is that the list of statements to be evaluated are being passed as a parameter to a function, in this function the variables are declared) I want to run through all of the items in the list and if it is a true statement then do something... Here is my code: (There is no output generated)</p>

<pre><code>a = ['b &gt; c', 'b = c', 'b &lt; c']

b = 5
c = 3

for item in a:
    if exec(item):
       print(item)
</code></pre>

<p><code>exec</code> is a function in python3.x<sup>1</sup> that returns <code>None</code> so you'll always have a falsy result.  You probably want <code>eval</code>.</p>

<p>Also be careful here.  Do not use this unless you completely trust the input strings as it will allow execution of arbitrary code otherwise.</p>

<hr>

<p>Note that this is a <em>very</em> strange code design and there is <em>probably</em> a better way to accomplish what you want...  For example:</p>

<p><em>Why</em> do you want to define the strings <em>before</em> the variables are defined?  Coupling strings to names in your code in this way is likely to lead to a painful code maintenance experience.</p>

<p><sup><sup>1</sup>In python2.x, this would fail with a <code>SyntaxError</code> since <code>exec</code> was a <em>statement</em> prior to python3.x</sup></p>

<hr>

<p>Having tried to understand your use-case a little more, I would propose that you create an API where you pass functions.</p>

<pre><code>def f1(a, b, **kwargs):
    return a &gt; b

def f2(a, b, **kwargs):
    return a == b

def f3(a, c, **kwargs):
    return a &lt;= c

funcs = [f1, f2, f3]
</code></pre>

<p>Now you can define a function that will pass the parameters.  You'll need to define <em>which</em> parameters it intends to pass -- but it will always pass them all:</p>

<pre><code>def func_caller(funcs):
    param_map = {
        'a': get_a_somehow(),
        'b': get_b_somehow(),
        'c': get_c_somehow(),
        ...
    }
    for func in funcs:
        if func(**param_map):
            print("Hello World!")
</code></pre>

<p>There are other way to make the "contract" between <code>func_caller</code> and the functions that it is calling even more binding (e.g. pass the params as a more structured object like a <code>namedtuple</code>).</p>

<pre><code>from collections import namedtuple
FuncCallerParams = namedtuple('FuncCallerParams', 'a,b,c')

def f1(func_caller_params):
   return func_caller_params.a &gt; func_caller_params.b
...

funcs = [f1, f2, ...]

def func_caller(funcs):
    a = ...
    b = ...
    c = ...
    fcp = FuncCallerParams(a, b, c)
    for func in funcs:
        if func(fcp):
           ...
</code></pre>

python string list loops if-statement