0

I would like to create multiple instances of the Product Class and convert each instance as a new item in a dictionary.

I'm not pretty sure how to append the new object and its attribute values to the dictionary

This is my class:

class Product():
    def __init__(self, id, name, descr):
        self.name = name
        self.id = id
        self.descr = descr

This is the part that creates the Product objects and inserts them into the dictionary:

addProductToDict(id, name, descr, product_dict):

    new_product = Product(id, name, descr)

    # insert new product instance into dict
    product_dict <--- new_product ### pseudo code

    return product_dict

    
product_dict = {} 

while True:
    print(" Give the products id, name and description")
    id = input("Give id: ")
    name = input("Give name: ")
    descr = input("Give descr: ")

    product_dict = addProductToDict(id, name, descr, product_dict)
        

Desired dictionary format:

my_dict = {'1': {'id': '1', 'name': 'TestName1', 'descr': 'TestDescription1'}, '2': {'id': '2', 'name': 'TestName2', 'descr': 'TestDescription2'}, '3': {'id': '3', 'name': 'TestName3', 'descr': 'TestDescription3'}}
7
  • 2
    I am not quite following what you want the structure of the dictionary to be. You can't just append an object. You could create an entry with a key:value where the value is the object but it is unclear what you want the key to be. Commented Mar 4, 2022 at 22:55
  • I edited my question to clarify the end result. Commented Mar 4, 2022 at 23:04
  • So if you have multiple objects, what do you expect your dictionary to be like? Commented Mar 4, 2022 at 23:13
  • 1
    Do you want to unpack the object into the dictionary? The desired output shows that, but "store each instance as a new item in a dictionary" would mean something different. What is the desired result for two objects? Commented Mar 5, 2022 at 8:56
  • Correct. The word "store" might cause some confusion. I edited my code to give more context. The end result should add each Object instance into the dictionary. Commented Mar 5, 2022 at 19:53

2 Answers 2

1

Given your desired output, I have modified my answer.

pprint(vars(new_product))

class Product():
    def __init__(self, id, name, descr):
        self.name = name
        self.id= id
        self.descr= descr

product_dict = {} 
new_product = Product(1, 'Test Name', 'Test Description')
product_dict = pprint(vars(new_product))

This will give you the desired format but I think you will have issues if you have more than one item in your dictionary.

Sign up to request clarification or add additional context in comments.

Comments

1

Perhaps you want to store them in a list instead of a dictionary, unless you have a key for each object

products = []
products.append(Product(1, 'Test Name', 'Test Description'))

edit

so, if you have a key

products = {}
_id = 1
products[_id] = Product(_id, 'Test Name', 'Test Description')

1 Comment

I have a key for each one the id attribute.

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.