0

I have a class called Order that basically just creates all of the parameters as instance objects (for now). There is a class variable called order_num which is the number of orders (increases when each object is created). I want the name of each object created to be the number of orders plus one. I have a variable next_order_num for that. I need to have the name of the object being created as the numerical value for next_order_num. How would you recommend doing that?

Here is the code:

next_order_num = Order.order_count + 1

O1 = Order(next_order_num, '10/10/22', 1000, 1000, 275, '1234 street road, 12345', 25, False)

Thanks!

2
  • 1
    If you mean you want order1 = Order(1, ...), order2 = Order(2, ...), etc. I would recommend storing the orders in a list instead and storing the order number in the Order itself. Please clarify since 1 isn't a valid object name in your example. What do you mean by "creates all parameters as instance objects"? Commented Oct 11, 2022 at 4:01
  • 1
    A variable name must be a valid identifier but a number isn't one. Commented Oct 11, 2022 at 4:01

1 Answer 1

1

Nothing in python can start with a number. You could keep two lists one of numbers and the other with the order instances. This way you can use the order number as an index for the second list.

order_nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# These would be real orders not strings
orders = ["order1", "order2", "order3", "order4", "order5",
          "order6", "order7", "order8", "order9", "order10", ]

for i in range(len(order_nums)):
    print(orders[i])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.