1

I'm using the graphics.py lib and need to create a number of polygons based on how many the user wants (lets say 4).

I just need to know how create a unique name for each of these polygons such as polygon1, polygon2, polygon4, polygon5.

Thanks.

  • 7
    [You don't want to dynamically create variables](http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html), because you want to [keep data out of your variable names](http://nedbatchelder.com/blog/201112/keep_data_out_of_your_variable_names.html). Just use a `list` of polygon objects, and then you can refer to them as `polygons[2]`, and so on. – abarnert Oct 25 '13 at 02:05
  • Keep them in a list/dict/set depending on how you need to access them – John La Rooy Oct 25 '13 at 02:07
  • possible duplicate of [In Python, how to create a large number of variables in a loop?](http://stackoverflow.com/questions/8129190/in-python-how-to-create-a-large-number-of-variables-in-a-loop) – jscs Oct 25 '13 at 02:20

3 Answers3

2

If you really wanted to do this, you could:

for i in range(count):
    new_poly = Polygon(whatever)
    new_name = 'polygon{}'.format(i+1)

Then you have to store that new name and that new polygon somewhere. Maybe like this:

polygons = {}
for i in range(count):
    new_poly = Polygon(whatever)
    new_name = 'polygon{}'.format(i+1)
    polygons[new_name] = new_poly

If you were thinking you'd rather just make each name a new variable… that's a bad idea. You don't want to dynamically create variables, because you want to keep data out of your variable names.

And really, there's no need for names here at all. This creates a list of count polygons:

polygons = [Polygon(whatever) for _ in range(count)]

And now, if you need to access them by number, just do this:

polygons[2]

(although be aware that lists are 0-based, not 1-based).

Or, if you need to access all of them, you've got the whole list, so you don't have to keep writing (polygon1, polygon2, polygon3, polygon4) everywhere; just polygons.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Yikes, i neglected to mention that the reason i'm using a loop is so that i can manipulate the attributes of the Polygons (Shifting their coordinates down 1, over 1) as i go about creating them. – MassDiffraction Oct 25 '13 at 02:47
  • 1
    And, so? I think this solution still works fine.. And if that's so, you may don't even need to create the variables dynamically. Just assign them in my loop. – aIKid Oct 25 '13 at 02:52
  • I'll work on it a bit and get back to you. Thank you. – MassDiffraction Oct 25 '13 at 02:55
  • @MassDiffraction: That's exactly what a list is perfect for: You can loop over it as many times as you need to do things like `for polygon in polygons: polygon.shift(1, 1)`. – abarnert Oct 25 '13 at 17:35
1

You could handle it as a list

polygon = []
polygon.append(...)

And access them as

polygon[2]
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
1

Bad way:

You can use exec.

for i in range(5):
    exec("polygon%d = Polygon(something)"%i)

This is pretty safe, since i if is only an integer. But using exec is a really bad practice, since it very vulnerable. User can enter, or accidentally enter, a piece of code and mess up with your memory. So, unless you really have to do it, you shouldn't use it.

Also, if you are a fan of global variables, you can use this.

globals()["polygon%d"%i] = Polygon(something)

But using global vars too often isn't recommended either.

Nicer way

Alternatively, you can store it in a list, or a dictionary:

for i in range(5):
    lst.append(Polygon(something_else)) #store it in a list
    dict['polygon%d'%i] = Polygon(another_thing) #store it in a dict

Hope this helps!!

aIKid
  • 26,968
  • 4
  • 39
  • 65
  • 1
    `exec` is _doubly_ bad. If you really want to dynamically create variables, use `globals()[name] = value` (or `setattr(self, name, value)`, etc.), not `exec`. – abarnert Oct 25 '13 at 02:09
  • No, there's no reason to even mention `exec` or `globals`. Just say use a list or a dict and leave it at that. – John Kugelman Oct 25 '13 at 02:16
  • I don't know that this deserves the downvotes, but it would be a lot better if you reorganized it. This implies that `global` is a "nicer way", when really you should say that the list or dict is the nice way, globals is the very bad way, and exec is the even-worse way… – abarnert Oct 25 '13 at 02:17
  • Now that's bad. Why the downvote? I also mentioned to use lists and dictionaries, the others are a bonus. Maybe a bad bonus though.. – aIKid Oct 25 '13 at 02:17
  • @abarnert Well, i'll just follow your advice. You're quite a nice teacher. – aIKid Oct 25 '13 at 02:19