Write a python program to demonstrate object as argument with default values

In this source code example, we will write a python program to demonstrate an object as an argument with default values.

Write a python program to demonstrate an object as an argument with default values

class Products:

    # object as arg - 2 default arg 
    def __init__(self,item,quantity,productList = {}):
        self.item = item
        self.quantity = quantity
        self.productList = productList

    def addProducts(self):
        self.productList[self.item] = self.quantity
        return self.productList


p = Products('Shampoo', 2)
print(p.addProducts())
p = Products('Soap', 2)
print(p.addProducts())

Output:

{'Shampoo': 2}
{'Shampoo': 2, 'Soap': 2}

Comments