New to Python, Got an error that i am unsure how to fix. Trying to answer a home work question. I am a bit confused on how to test the class and its methods, Which is part 2 of the question. Is there a special way of doing it?
Write a class to represent a Product as per following specifications:
Class name: Product (note uppercase "P")
Define instance attributes as specified by the items in dictionary object product2 i.e. discontinued, lead_time_days, etc.
Define an init() method which initialises the instance attributes to the value of the parameters passed to the method
Define a set_product_name() method which sets the product_name in the instance variable to the value of the parameter passed
Define a get_product_details() method which returns the product details formatted as "product_name - product_description"
After having defined the class, you need to test the class and its methods to show that it works. You can do this as follows:
Instantiate an object of the class (name it prod) and pass product2 to the initialiser (pass the whole product2 object, not individual attributes)
Set the product_name to something different by calling set_product_name() and passing it an appropriate parameter
Call get_product_details() to get the product name and description (and print it)
Code:
product2 = {
"discontinued": 0,
"lead_time_days": 4,
"product_category": "Personal Computers",
"product_description": "8 inch Display (1920x1200) ...",
"product_id": 104,
"product_name": "NVIDIA SHIELD Tablet (WiFi)",
"reorder_level": 10,
"unit_price": 299.0
}
# My Code
class Product:
def __init__(self, discontinued, lead_time_days, product_category, product_description, product_id, product_name, reorder_level, unit_price):
self.discontinued = discontinued
self.product_category = product_category
self.product_description = product_description
self.product_id = product_id
self.product_name = product_name
self.reorder_level = reorder_level
self.unit_price = unit_price
def set_product_name(self, value):
self.product_name = value
def get_product_details(self):
return self.product_name + " " + self.product_description
Edit 1
I tried this but i know its wrong.
prod = Product(product2["discontinued"], product2["product_category"], product2['product_description'], product2['product_id'], product2['product_name'],product2['reorder_level'],product2['unit_price'])
prod.set_product_name('star')
print ('get_product_details'())
-
You don't show your whole code so we can't see how you are creating the object. Try something like (2 lines) x = Product(0, 4, 'Personal Computers', '8 inch', 104, 'Nvidia', 10, 299.0) and then print product.get_product_details()David Freeman– David Freeman2019年08月21日 23:09:18 +00:00Commented Aug 21, 2019 at 23:09
-
Edited my original post, not sure if that will help.Rikio– Rikio2019年08月21日 23:14:31 +00:00Commented Aug 21, 2019 at 23:14
3 Answers 3
Pro tip: use unpacking to deliver keyword arguments to a class method.
This can be done by changing your class to the following:
class Product:
def __init__(self, discontinued=None, lead_time_days=None, product_category=None, product_description=None, product_id=None, product_name=None, reorder_level=None, unit_price=None):
self.discontinued = discontinued
self.product_category = product_category
self.product_description = product_description
self.product_id = product_id
self.product_name = product_name
self.reorder_level = reorder_level
self.unit_price = unit_price
def set_product_name(self, value):
self.product_name = value
def get_product_details(self):
return self.product_name + " " + self.product_description
By giving your arguments default values (e.g. discontinued=None) it makes them keyword arguments. This is helpful if you have a dictionary or a large number of inputs to a function.
To create the object/instantiate the class you can call:
obj = Product(**product2)
**product2 basically means:
discontinued=0,lead_time_days=4,product_category="Personal Computers",product_description="8 inch Display (1920x1200) ...",product_id=104,product_name="NVIDIA SHIELD Tablet (WiFi)",reorder_level=10,unit_price=299.0
After the object is instantiated you call its methods using dot notation:
obj.set_product_name('new name')
print(obj.get_product_details())
1 Comment
product2 can be passed as kwargs dict.
Then you can call your function for the instance you created:
prod = Product(**product2)
prod.set_product_name('star')
print (prod.get_product_details())
star 8 inch Display (1920x1200) ...
Comments
1. Your testing code doesn't comply to the requirement "pass the whole product2 object, not individual attributes". You pass individual attributes:
prod = Product(product2["discontinued"], product2["product_category"], product2['product_description'], product2['product_id'], product2['product_name'],product2['reorder_level'],product2['unit_price'])
2. The setattr(object, name, value) function will be handy in your case.
My solution:
product2 = {
"discontinued": 0,
"lead_time_days": 4,
"product_category": "Personal Computers",
"product_description": "8 inch Display (1920x1200) ...",
"product_id": 104,
"product_name": "NVIDIA SHIELD Tablet (WiFi)",
"reorder_level": 10,
"unit_price": 299.0
}
class Product:
def __init__(self, specification):
for key, value in specification.items():
setattr(self, key, value)
def set_product_name(self, value):
self.product_name = value
def get_product_details(self):
return f'{self.product_name} - {self.product_description}'
###Test
# Instantiate an object of the class (name it prod) and pass product2 to
# the initialiser (pass the whole product2 object, not individual attributes)
prod = Product(product2)
# Set the product_name to something different by calling set_product_name()
# and passing it an appropriate parameter
prod.set_product_name('Another Tablet')
# Call get_product_details() to get the product name and
# description (and print it)
print(prod.get_product_details())