In your example, p1, p2, p3
are identifiers, but in your for
-loop, you add the names of these identifiers as strings to your list. Consequently, you obtain a list of strings, not a list of dereferenced values for identifiers that matched these strings. This behavior is intended and desired.
Identifiers are kind of named handles you use in your code to access the values they reference. In general, no programming language automatically converts strings that may match some local identifiers into actual references, as this is unintended behavior in most cases. Think of scope and security.
Usually, you should not try to convert strings into identifiers. In python, you can do so using getattr
. However, I'll not outline this here, because, again, you should not do so.
The better approach for your case is to directly instantiate your points in a list
or dict
structure:
# List:
plist = [(20, 50), (70, 80), (100, 140)]
# Dict:
pdict = {
'p1': (20, 50),
'p2': (70, 80),
'p3': (100, 140)
}
In these cases, you can access your points like so:
# For list case:
for i in range(3):
print(plist[i])
# For dict case:
for i in range(3):
print(pdict['p{:d}'.format(i)])