-2

I tried to write code as below:

for i in range(1,13):
    'test{}'.format(i) = """
select * from test_table where month(date_time) = {}
""".format(i)

But I got the following syntax error: SyntaxError: can't assign to function call.

How can I fix this?

krmogi
  • 2,588
  • 1
  • 10
  • 26
Elsa
  • 1
  • 1
  • 8
  • 27
  • 4
    What are you trying to do? Are you trying to assign to variables named `test1`, `test2` etc.? – Passerby Nov 30 '21 at 03:59
  • Yes, I would like to assign to variables named test1, test2 – Elsa Nov 30 '21 at 04:01
  • 1
    While what you are trying is possible, it's messy, consider just appending each result to a list. – Passerby Nov 30 '21 at 04:02
  • please read this [this](https://stackoverflow.com/help/how-to-ask) before you ask a question. – HamzaFarooq Nov 30 '21 at 04:06
  • @HamzaFarooq What's wrong with my question? – Elsa Nov 30 '21 at 04:07
  • It seems like you are trying to make variable variable names. That's almost always the wrong choice — you should be using a dictionary or list. [see this question](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – Mark Nov 30 '21 at 04:11
  • You will better know after you read the guide I've attached earlier in the comment. – HamzaFarooq Nov 30 '21 at 04:11

1 Answers1

0

You can use a dictionary instead:

d = {}
for i in range(1, 13):
    d["test{0}".format(i)] = "select * from test_table where month(date_time) = {0}".format(i)

print(d)

Output:

{'test1': 'select * from test_table where month(date_time) = 1', 'test2': 'select * from test_table where month(date_time) = 2', 'test3': 'select * from test_table where month(date_time) = 3', 'test4': 'select * from test_table where month(date_time) = 4', 'test5': 'select * from test_table where month(date_time) = 5', 'test6': 'select * from test_table where month(date_time) = 6', 'test7': 'select * from test_table where month(date_time) = 7', 'test8': 'select * from test_table where month(date_time) = 8', 'test9': 'select * from test_table where month(date_time) = 9', 'test10': 'select * from test_table where month(date_time) = 10', 'test11': 'select * from test_table where month(date_time) = 11', 'test12': 'select * from test_table where month(date_time) = 12'}

Now you can access a certain key like:

print(d['test3'])

to get a certain value:

select * from test_table where month(date_time) = 3

You could also do something like:

for i in range(1, 13):
    globals()['test%s' % i] = "select * from test_table where month(date_time) = {0}".format(i)

print(test3)

to get

select * from test_table where month(date_time) = 3

but using globals is a bad idea, usually dictionaries or lists should be used in these scenarios.

krmogi
  • 2,588
  • 1
  • 10
  • 26
  • Thanks so so so much for your detailed answer!!! – Elsa Nov 30 '21 at 06:07
  • Hi, you said that's a bad practice, which one would be a good practice? I want to solve my problem, and I wanna a good one – Elsa Nov 30 '21 at 06:21
  • The first solution is more natural and easier to understand so that’s why it’s the better solution. The second one is harder to understand and less natural, so I would prefer to use the first solution. – krmogi Nov 30 '21 at 15:29
  • 1
    Got it, thanks so so so much for your help!!!!! Have a nice day – Elsa Dec 01 '21 at 03:19