0

i have this code:

 if opt1 is not None: user_a, db_a = opt1.split("/") db_a = country_assoc(int(db_a)) client_a = Client(None, user_a, db_a) data_client_a = client_a.get_user() if opt2 is not None: user_b, db_b = opt2.split("/") db_b = country_assoc(int(db_b)) client_b = Client(None, user_b, db_b) data_client_b = client_b.get_user() .... 

But, i want to generate a similar structure with a loop.

What is the correct way to do that? I am trying this

 abcde = ['a', 'b', 'c', 'd', 'e'] for idx, val in enumerate(abcde): if opt+idx is not None: user_+val, db_+val = opt+idx.split("/") db_+val = country_assoc(int(db_+val)) client_+val = Client(None, user_+val, db_+val) data_client_+val = client_+val.get_user() 

2 Answers 2

5

I don't understand why people feel the need to try and do this sort of thing. Variable names are not data. There is never a good reason to create variable names dynamically.

Simply put your values in a dictionary or a list.

for idx, val in enumerate(abcde): if opts[idx] is not None: user, db = opts[idx].split("/") users[val] = user dbs[val] = country_assoc(int(db)) clients[val] = Client(None, user, db) data_clients.append(clients[val].get_user()) 
Sign up to request clarification or add additional context in comments.

Comments

0
a = [opt1, opt2, opt3, opt4, opt5] data_clients = [] for j in a: if j is not None: user, db = j.split("/") db = country_assoc(int(db)) client = Client(None, user, db) data_clients.append(client.get_user()) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.