2

I need to check variables looking like this:

if name1 != "":
    (do something)

Where the number right after "name" is incremented between 1 and 10.

Do I need to write the test ten times or is there a way (without using an array or a dict) to "concatenate", so to speak, variable names?

I'm thinking about something like this:

for i in range(10):
    if "name" + str(i) != "":
        (do something)

Edit: I can't use a list because I'm actually trying to parse results from a Flask WTF form, where results are retrieved like this:

print(form.name1.data)
print(form.name2.data)
print(form.name3.data)
etc.
Rao
  • 20,781
  • 11
  • 57
  • 77
Lucien S.
  • 5,123
  • 10
  • 52
  • 88
  • 1
    You use a list to hold your values, not ten different variables! – kindall Apr 27 '16 at 19:15
  • 1
    Is there a reason you can't use a list or a dict for this? It would be the most straightforward solution – StephenTG Apr 27 '16 at 19:16
  • 7
    @Rodolphe You should explain _why_ you can't use a list. This just sounds like [the XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). In almost all cases, using `eval` to do something like this means you have a terrible software design issue. – Vincent Savard Apr 27 '16 at 19:21
  • In the **specific case** where there are **already existing** attributes of an object like `form.name1`, `form.name2` etc. **and** you can't modify the code (e.g. to make `form.name` be a list of those values), the appropriate tool is `getattr`. I have added an appropriate duplicate. In general, use a list or dictionary to avoid the problem. – Karl Knechtel Jul 04 '22 at 22:58

3 Answers3

4
  1. Use a list, such as:

    names = ['bob', 'alice', 'john']
    

    And then iterate on the list:

    for n in names:
      if n != "":
         (do something)
    
  2. or you could have a compounded if statement:

    if (name1 != "" or name2 != "" or name3 != "")
    

The best solution would be to use solution #1.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Milean
  • 878
  • 9
  • 16
3

If you cannot use a list or a dict, you could use eval

for i in range(10):
    if eval("name" + str(i)) != "":
        (do something)
Francesco
  • 4,052
  • 2
  • 21
  • 29
  • Few notes 1. [Is Using eval In Python A Bad Practice?](http://stackoverflow.com/q/1832940) 2. [Why should exec() and eval() be avoided?](http://stackoverflow.com/q/1933451) 3. [Ned Batchelder: Eval really is dangerous](http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html) – Bhargav Rao Apr 27 '16 at 19:35
  • [**Do not ever use `eval` (or `exec`) on data that could possibly come from outside the program in any form. It is a critical security risk. You allow the author of the data to run arbitrary code on your computer.**](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice) – Karl Knechtel Jul 04 '22 at 22:53
0

First of all, your app have invalid logic. You should use list, dict or your custom obj.

You can get all variable in globals. Globals is a dict.

You can do next:

for i in range(10):
    if globals().get('name%d' % i):
        # do something
mrvol
  • 2,575
  • 18
  • 21