Automatically numbered variable names declared as nonlocal

To create variables x1, x2, …, x10, use:
for i in range(1,10):
Then how to declare these as nonlocal ones to use them as global variables in functions?

To rephrase this question, I need to define variables x1, x2, …, xn over a for loop where n is a control parameter, that is, n is given a value elsewhere. Afterwards, these variables need to function normally as those defined in usual ways. What’s the proper way of defining them?

Define a function and return them to the global scope.

hi @ptrblck many thanks. I don’t know exactly what to do. How does this code look?

def function(n):
for i in range(1,n):
return global locals()[‘x’+str(i)]

Use a dict instead of trying to create variables in different scopes:

def fun():
    ret = {}
    for i in range(10):
        ret["x"+str(i)] = i
    return ret

x = fun()
# {'x0': 0, 'x1': 1, 'x2': 2, 'x3': 3, 'x4': 4, 'x5': 5, 'x6': 6, 'x7': 7, 'x8': 8, 'x9': 9}