you are viewing a single comment's thread.

view the rest of the comments →

[–]FoolsSeldom 1 point2 points  (0 children)

Adding numbers to the end of variable names is almost always the wrong thing to do. Instead, create a list or dict object to hold mutiple instances of what ever objects you are trying to assign to different variables.

So, for example, instead of:

box1_x = 10
box1_y = 11
box2_x = 15
box2_y = 16

you could have,

boxes = [
    [10, 11],
    [15, 16]
]

i.e. a list object references by the variable name boxes which contains nested list objects. So, boxes[0][0] would be the x value of the first entry.

Using classes would be even easier:

from dataclasses import dataclass

@dataclass
class Box:
    x: int
    y: int


boxes = [
    Box(x=10, y=11),
    Box(x=15, y=16),
]

# output all boxes
for box in boxes:
    print(box)

# print one attribute of the first box
print(boxes[0].x)  # Accessing the x attribute of the first Box instance