0

I can't seem to turn a string into a corresponding variable.

point1 is a defined tuple (100, 100), but once I combined "point" and 1, it stays a string and doesn't return (100, 100).

I'd like to know if there's a way to make the string that existing variable, or if there's a way to combine "point" and "1" without turning either of them into strings. (I combined "point" and "1" using format.)

point1 = (100, 100)

def point(step):
    return "point{}".format(step)

print(point1)
print(point(1))
print(tuple(point(1)))

I want "point(1)" to return "(100,100)" but it returns "point1".

.

Edit(??): Thank you so much everyone! All of these answers helped so much! ^^

My question is unique from the variables variables question, because this one is solved simply by putting the string inside eval() or globals().

eg:

point1 = (100,100)

print(point1)
print("point1")
print(eval("point1"))

# or

def point(step):
    return globals()[f"point{step}"]

print(point(1))

This gives me

(100, 100)

point1

(100, 100)

(100, 100)

Cambria
  • 3
  • 2
  • 1
    You can evaluate strings as python expressions but there is almost always a better way – juanpa.arrivillaga Oct 14 '19 at 02:49
  • Possible duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Craig Oct 14 '19 at 02:52
  • Thank you so much! I'm very new to python and coding in general. I'll look this up, and if I still have problems, I guess I'll ask around. – Cambria Oct 14 '19 at 02:52
  • use list or dictionary to keep many values. – furas Oct 14 '19 at 02:53

3 Answers3

0

It seems you’re trying to access the global variable using a string. Using globals() will safely do this. Using eval can be very bad especially when passing user input to it.

def point(step):
    return globals()[f"point{step}"]

Although this seems you’re trying to access a variable amount of points. If so why not use a list:

points = [(100,100), ...]

Then to access the first point just use

points[0]
Jab
  • 26,853
  • 21
  • 75
  • 114
  • Oh, I didn't know that lists can contain tuples too. Thanks so much, I'll probably use this a lot for my code! :D – Cambria Oct 14 '19 at 03:14
0

You can use eval for this, normally considered a litle risky but generally okay provided you can control what it's actually evaluating:

def point(step):
    try:
        return eval("point{}".format(step))
    except:
        return None

point1 = (100, 100)
print(point1)
print(point(1))
print(point(2))

This outputs:

(100, 100)
(100, 100)
None

If you would rather receive an exception than default to None, simply remove the try/except stuff:

def point(step):
    return eval("point{}".format(step))

Of course, if the intent here is to have a number of points (where you may not know the quantity in advance), Python has lists to do exactly that:

point = [(100, 100), (7, 42)]

Then you can reference point[0] and point[1] to get the individual tuples.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Looks like you're trying to get the value by string:

def point(step):
    v = globals()[f"point{step}"]
    return f"{v}"

Then

print(point(1))

will print

(100, 100)
dmitryro
  • 3,463
  • 2
  • 20
  • 28
  • Whoa that's a cool way to do it! I might use this way because eval() is apparently evil(??). I don't know why though. Thanks so much for the help! – Cambria Oct 14 '19 at 03:12