0

I'm looking for a way, in python 3.2, to create a large number of variables and assign them values. Something like

X = 10
Y = 10
A = 0
B = 0
while X >= 0:
    while Y >= 0:
        cell[C]X[A] = A
        cell[C]Y[B] = B
        B = B + 1
        Y = Y - 1
        C = C + 1
    A = A + 1
    X = X - 1

Which would optimally create 200 variables of cell1X1, cell1Y1, cell2X1, cell2Y2, etc etc etc.

Is this possible? How would it be done?

Please keep in mind that I'm still very new to python, so please make things as simple as possible.

Also, while I understand that there are other ways to do this, and they they are probably better, I still want to know how to do things this way.

I understand dictionaries might be better in every possible way, but that's not what I'm asking for.

Thanks for the help.

Edit: When I say new to python, I meant to say new to programming in general. Like very new. Like, just recently learned how to write functions.

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
Luingar
  • 39
  • 1
  • 4
  • for one because dictionaries are a bit more complicated and i don't quite understand them yet. For 2 because i'm still learning and i want to experiment and do things wrong so i know WHY they're wrong. And as for your example, i don't think that's quite the same question as i'm asking... – Luingar Nov 14 '11 at 22:42
  • 3
    Dictionaries are emphatically *not* more complicated than whatever monstrosity you might end up with that is capable of achieving this in the way that you have in mind. – recursive Nov 14 '11 at 22:46
  • 2
    Since you're asking a question that can reasonably be answered with "**Don't do that**," you might get better answers if you give a fuller explanation of why you want to do this. Working with existing (braindead) code? Joy of learning? Masochism? – blahdiblah Nov 14 '11 at 22:47
  • 2
    #1 is a lame reason. They're very simple and you're basically already treating the local scope as a dictionary (it isn't!). #2 is a honorable goal, but a major part of the objections, at least for me, are style and self-discipline issues, which you may not agree with (yet) and which may not show their full scale in the small programs you write now. Sure, you can experiment with it, but I'd think thrice before introducing it in any project, and I *especially* wouldn't postpone getting comfortable with the proper alternatives for it. –  Nov 14 '11 at 22:51
  • 1
    On the bright side, if you manage to get this working without using dictionaries, learning about dictionaries will be a joy. – recursive Nov 14 '11 at 23:04
  • 1
    You haven't said what you are going to DO with these 200 variable after you have created them ... – John Machin Nov 15 '11 at 11:27

4 Answers4

10

Honestly, the only reasonable answer to this question is: Don't do that, and learn how to use dictionaries instead.

I believe this is also the most helpful answer as well. You have already stated in your question that (1) you are new to Python, (2) you are new to programming in general, and (3) you accept that dictionaries are possibly better in every way to what you are asking.

You say you are asking "for learning purposes". Well, the more productive approach is to learn a bit more of the language first, before insisting on a way to do something that you acknowledge is not encouraged by the language itself, and certainly not encouraged by anyone who is offering their comments and answers.

In this case, learn how dictionaries work. I think you will not find them that complicated. Even if you do, it will be worth your time and effort to really understand them anyway, because dictionaries are a very fundamental feature of Python. They are used all over the place in Python, and even if you don't stick with Python, many other languages use the same concept very heavily (though often using a different name, such as "associative array", "hash", "table", or perhaps other things).

After you are comfortable with dictionaries themselves, if you still can't figure out how to apply them to some specific programming problem, then maybe it will be time to ask another question.

John Y
  • 14,123
  • 2
  • 48
  • 72
5

Like everyone else is saying: don't do that. Unfortunately, there is no way for your program to have the names of variables themselves depend on variables (actually, there are ways to do this, but it's complicated and rarely the right way to do this).

It's probably important to point out that the reason you shouldn't do that is not merely that it's complicated in Python, but that wanting to do it that way indicates a fundamental misunderstanding of the distinction in most languages between code (including the names of things in the language) and data (the changeable values of things).

In this case, what you actually need are dictionaries, lists or something similar:

At least in your example, you've only got two sorts of things, which you call cell[C]X[A] and cell[C]Y[B]. You could just make these into cellX[A,B] and cellY[A,B] which are perfectly legal constructions in python.

So before the loop, do this, which initialises an empty dictionary.

cellX = {}
cellY = {}

then, inside the loop, you can have

 cellX[C,A]=A
 cellY[C,B]=B

Also note that your loop, as it is now, isn't very "pythonic". You should consider looping like for X in range(10,-1,-1):

Andrew Jaffe
  • 26,554
  • 4
  • 50
  • 59
4

It's dark and hacky, but you could try:

 for i in range(100):
     locals()['A%i'%i] = i

But please don't do this. It's usually terrible practice.

As @delnan points out, it probably won't work on some python versions and implementations. locals() is the local namespace, which is part of Python's implementation. You don't really want to play with it.

Another more important reason why you don't want to do this - it's a security and reliability nightmare. You can't see what all the variable names will be, so it's possible you will accidentally overwrite one of your other variables. You are getting all these "variables" from the same place, so it's logical to put them all together in a dictionary.

I'll explain why it works, at least in some cases: the local namespace is often implemented as a dictionary, and locals() will sometimes return the local namespace dictionary, not a copy.

Did that make no sense at all?

Python variables are like items in a dictionary. There's one namespace for the module, one for the function, one "global" namespace, and so on. locals() gets you the most local namespace, and globals() gets you the global namespace.

If the local namespace is actually implemented as a dictionary, you may be able edit it by editing locals() (reaching into the guts of the python interpreter). HOWEVER, sometimes the local namespace is not implemented as a dictionary (for performance reasons) - this happens inside functions. In that case, locals() will give you a copy of the namespace, and you will not be able to edit it (or at least, it won't change the local namespace).

Still confused? Don't do it.

wisty
  • 6,981
  • 1
  • 30
  • 29
-1

Global scope is not something you want to create variables in, i can offer you couple of ways to create arrays instead:

There is a couple of ways you can take. one would be to multiply like so:

a = [1]*5

This will actually build you a list of references to the same array which is not what we need - good catch @Aaron Dufour

b = [[0]*5]*5

but here is a way that avoids creating references:

list(map(lambda x: [x]*6,[1]*5))

and so on. another way might be range in python 3 it returns itself so you can turn it into an array like so:

list(range(0,10))

another way might be writing an actual loop, if you need special values that are affected by the loop or something else you can write a lambda function inside a map call on an array.

A good start with python is this book: Dive into Python 3

Good luck!

edit: contaminating your global scope with variables is a bad practice - no matter what's the language, try to avoid it.

Leon Fedotov
  • 7,421
  • 5
  • 30
  • 33
  • 3
    Ahhhh! Don't do this! `[0]*5` is ok. `[[0]*5]*5` is NOT. You will have a list of 5 references to the same list. For example, if you assign `b` as shown and then say `b[0][0] = 1`, you will find that `b[1][0]`, `b[2][0]`, etc. are also equal to `1`. – Aaron Dufour Nov 14 '11 at 22:50
  • 1
    Re your edit: Even polluting the tiniest local scope is a bad practice. –  Nov 14 '11 at 22:52
  • @AaronDufour whoops - you are totally right! didn't think of it. – Leon Fedotov Nov 14 '11 at 22:55
  • 1
    thanks for the book link. Seriously. :D – Luingar Nov 14 '11 at 22:57