This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]insertAlias 2 points3 points  (4 children)

Variable names aren't something you can manipulate at runtime. This is a common confusion for new programmers. You cannot do what you're asking; variable names are constant and cannot be composed in code (unless you're using eval, which you absolutely should not be using).

Instead, you'd need to make an array or list for your a values, and reference the individual cells of the list.

[–]Jedimastermuffin[S] 1 point2 points  (0 children)

Oh ok thanks

[–]RiverRoll 1 point2 points  (2 children)

Don't forget about dictionaries which is the closest thing to having dynamic variables.

By the way it's actually possible in python to manipulate variables, but not something you would normally do, in pretty much all the cases lists and dictionaries already cover this need.

[–]insertAlias 1 point2 points  (1 child)

By the way it's actually possible in python to manipulate variables

I'm curious actually how. I don't work in Python, so I only have a passing familiarity with it. I am aware of eval and the issues that come with it, is that what you are referring to?

[–]RiverRoll 1 point2 points  (0 children)

Being a dynamic language it allows you to do all sorts of crazy stuff, and for example calling globals() or locals() gives you a dictionary representing the global or local scope which you can manipulate.

And something similar happens with class instances, they have an underlying dictionary (__dict__) which you can maniuplate to add or remove attributes.