Skip to main content
deleted 2 characters in body
Source Link
l0b0
  • 9k
  • 21
  • 36
  • Single letter names are not readable in general. i, p and q, for example, would be easier to read as identification, price, and quantity. This is also a common pattern, where parameters to be stored as fields are named the same as the fields.
  • The last three lines should be in a main function, called if __name__ == '__main__'.
  • item_dictionary would be better expressed either by making Product a dict subtype or making an as_dict method.
  • product_1 and product_2 should be passed as either a products array argument or a *products star argument to Inventory().
  • Why unpack the products in Inventory?
  • The code directly inside Inventory (for loop and print()) should be in a method. I've never even seen Python code with something other than method definitions at the top level.
  • Product quantity is a property of an inventory, not a product.

I would probably implement this as follows (untested):

class Product:
    def __init__(self, identification, price):
        self.identification = identification
        self.price = price

class Inventory:
    def __init__(self, products):
        self.products = products

    @property
    def value():
        return sum([productproduct.price for product in self.products]products)

def main():
    product_1 = Product(32, 23)
    product_2 = Product(23, 65)
    inventory = Inventory(2 * [product_1] + 3 * [product_2])
    print(inventory.value)

if __name__ == '__main__':
    main()
  • Single letter names are not readable in general. i, p and q, for example, would be easier to read as identification, price, and quantity. This is also a common pattern, where parameters to be stored as fields are named the same as the fields.
  • The last three lines should be in a main function, called if __name__ == '__main__'.
  • item_dictionary would be better expressed either by making Product a dict subtype or making an as_dict method.
  • product_1 and product_2 should be passed as either a products array argument or a *products star argument to Inventory().
  • Why unpack the products in Inventory?
  • The code directly inside Inventory (for loop and print()) should be in a method. I've never even seen Python code with something other than method definitions at the top level.
  • Product quantity is a property of an inventory, not a product.

I would probably implement this as follows (untested):

class Product:
    def __init__(self, identification, price):
        self.identification = identification
        self.price = price

class Inventory:
    def __init__(self, products):
        self.products = products

    @property
    def value():
        return sum([product.price for product in self.products])

def main():
    product_1 = Product(32, 23)
    product_2 = Product(23, 65)
    inventory = Inventory(2 * [product_1] + 3 * [product_2])
    print(inventory.value)

if __name__ == '__main__':
    main()
  • Single letter names are not readable in general. i, p and q, for example, would be easier to read as identification, price, and quantity. This is also a common pattern, where parameters to be stored as fields are named the same as the fields.
  • The last three lines should be in a main function, called if __name__ == '__main__'.
  • item_dictionary would be better expressed either by making Product a dict subtype or making an as_dict method.
  • product_1 and product_2 should be passed as either a products array argument or a *products star argument to Inventory().
  • Why unpack the products in Inventory?
  • The code directly inside Inventory (for loop and print()) should be in a method. I've never even seen Python code with something other than method definitions at the top level.
  • Product quantity is a property of an inventory, not a product.

I would probably implement this as follows (untested):

class Product:
    def __init__(self, identification, price):
        self.identification = identification
        self.price = price

class Inventory:
    def __init__(self, products):
        self.products = products

    @property
    def value():
        return sum(product.price for product in self.products)

def main():
    product_1 = Product(32, 23)
    product_2 = Product(23, 65)
    inventory = Inventory(2 * [product_1] + 3 * [product_2])
    print(inventory.value)

if __name__ == '__main__':
    main()
added 364 characters in body
Source Link
l0b0
  • 9k
  • 21
  • 36
  • Single letter names are not readable in general. i, p and q, for example, would be easier to read as identification, price, and quantity. This is also a common pattern, where parameters to be stored as fields are named the same as the fields.
  • The last three lines should be in a main function, called if __name__ == '__main__'.
  • item_dictionary would be better expressed either by making Product a dict subtype or making an as_dict method.
  • product_1 and product_2 should be passed as either a products array argument or a *products star argument to Inventory().
  • Why unpack the products in Inventory?
  • The code directly inside Inventory (for loop and print()) should be in a method. I've never even seen Python code with something other than method definitions at the top level.
  • Product quantity is a property of an inventory, not a product.

I would probably implement this as follows (untested):

class Product:
    def __init__(self, identification, price):
        self.identification = identification
        self.price = price

class Inventory:
    def __init__(self, products):
        self.products = products

    @property
    def value():
        return sum([product.price for product in self.products])

def main():
    product_1 = Product(32, 23)
    product_2 = Product(23, 65)
    inventory = Inventory(2 * [product_1] + 3 * [product_2])
    print(inventory.value)

if __name__ == '__main__':
    main()
  • Single letter names are not readable in general. i, p and q, for example, would be easier to read as identification, price, and quantity. This is also a common pattern, where parameters to be stored as fields are named the same as the fields.
  • The last three lines should be in a main function, called if __name__ == '__main__'.
  • item_dictionary would be better expressed either by making Product a dict subtype or making an as_dict method.
  • product_1 and product_2 should be passed as either a products array argument or a *products star argument to Inventory().
  • Why unpack the products in Inventory?
  • The code directly inside Inventory (for loop and print()) should be in a method. I've never even seen Python code with something other than method definitions at the top level.
  • Single letter names are not readable in general. i, p and q, for example, would be easier to read as identification, price, and quantity. This is also a common pattern, where parameters to be stored as fields are named the same as the fields.
  • The last three lines should be in a main function, called if __name__ == '__main__'.
  • item_dictionary would be better expressed either by making Product a dict subtype or making an as_dict method.
  • product_1 and product_2 should be passed as either a products array argument or a *products star argument to Inventory().
  • Why unpack the products in Inventory?
  • The code directly inside Inventory (for loop and print()) should be in a method. I've never even seen Python code with something other than method definitions at the top level.
  • Product quantity is a property of an inventory, not a product.

I would probably implement this as follows (untested):

class Product:
    def __init__(self, identification, price):
        self.identification = identification
        self.price = price

class Inventory:
    def __init__(self, products):
        self.products = products

    @property
    def value():
        return sum([product.price for product in self.products])

def main():
    product_1 = Product(32, 23)
    product_2 = Product(23, 65)
    inventory = Inventory(2 * [product_1] + 3 * [product_2])
    print(inventory.value)

if __name__ == '__main__':
    main()
added 364 characters in body
Source Link
l0b0
  • 9k
  • 21
  • 36
  • Single letter names are not readable in general. i, p and q, for example, would be easier to read as identification, price, and quantity. This is also a common pattern, where parameters to be stored as fields are named the same as the fields.
  • The last three lines should be in a main function, called if __name__ == '__main__'.
  • item_dictionary would be better expressed either by making Product a dict subtype or making an as_dict method.
  • product_1 and product_2 should be passed as either a products array argument or a *products star argument to Inventory().
  • Why unpack the products in Inventory?
  • The code directly inside Inventory (for loop and print()) should be in a method. I've never even seen Python code with something other than method definitions at the top level.
  • Single letter names are not readable in general. i, p and q, for example, would be easier to read as identification, price, and quantity. This is also a common pattern, where parameters to be stored as fields are named the same as the fields.
  • The last three lines should be in a main function, called if __name__ == '__main__'.
  • item_dictionary would be better expressed either by making Product a dict subtype or making an as_dict method.
  • Single letter names are not readable in general. i, p and q, for example, would be easier to read as identification, price, and quantity. This is also a common pattern, where parameters to be stored as fields are named the same as the fields.
  • The last three lines should be in a main function, called if __name__ == '__main__'.
  • item_dictionary would be better expressed either by making Product a dict subtype or making an as_dict method.
  • product_1 and product_2 should be passed as either a products array argument or a *products star argument to Inventory().
  • Why unpack the products in Inventory?
  • The code directly inside Inventory (for loop and print()) should be in a method. I've never even seen Python code with something other than method definitions at the top level.
Source Link
l0b0
  • 9k
  • 21
  • 36
Loading