EDIT: This got flagged as a duplicate because of my asking about how to properly print a class item. That was part of my question yes, but I was more confused about the interaction of object instances and the way things are referenced in general. This has been helpful. Also, I should have done it originally, but I've included MITx's (online mooc) solution for the 'bag' creation of the 0/1 knapsack problem I've been learning. It probably makes it more clear why they wrote the class
and buildItems
function as they did.
I've read a gazillion tutorials now to try to understand the object referencing and creation of class instances in python and can't figure out why I'm getting this particular output that I haven't seen.
I have this program snippet:
class Item(object):
def __init__(self, n, v, w):
self.name = n
self.value = float(v)
self.weight = float(w)
def getName(self):
return self.name
def getValue(self):
return self.value
def getWeight(self):
return self.weight
def __str__(self):
return '<' + self.name + ', ' + str(self.value) + ', '\
+ str(self.weight) + '>'
def buildItems():
return [Item(n,v,w) for n,v,w in (('clock', 175, 10),
('painting', 90, 9),
('radio', 20, 4),
('vase', 50, 2),
('book', 10, 1),
('computer', 200, 20))]
EDIT (rest of code)
def yieldAllCombos(items):
N = len(items)
# Enumerate the 3**N possible combinations
for i in range(3**N):
bag1 = []
bag2 = []
for j in range(N):
if (i // (3 ** j)) % 3 == 1:
bag1.append(items[j])
elif (i // (3 ** j)) % 3 == 2:
bag2.append(items[j])
yield (bag1, bag2)
Edit end
These are the parts of a introductory knapsack problem I've been working on, this problem made me realize I didn't understand what is really going on with classes and object instances.
If I understand buildItems()
, it will create 6 different instances of the Item
class and return a list containing those instances.
Trying to do something simple, I want to create that single list containing those default values in the buildItems()
function. Just to test I do this:
myitems = buildItems()
print(myitems)
I'm expecting to see a list of lists like this:
[[clock,175,10], [painting,90,9], [radio,20,4], [vase, 50, 2], [book, 10, 1], [computer, 200, 20]]
What I get back though is:
[[<__main__.Item object at 0x7f2ce7819650>, <__main__.Item object at 0x7f2ce67cfa90>, <__main__.Item object at 0x7f2ceb766a50>, <__main__.Item object at 0x7f2ceb766d10>, <__main__.Item object at 0x7f2cebc9ba90>, <__main__.Item object at 0x7f2ceb4ec210>]]
Which I realize are the actual objects and there locations in memory, correct? What do I need to do return the actual data? Also, I have tried putting both buildItems
inside and outside the class definition, if I put it inside, it requires me to include the self
in the def buildItems(self)
, but then more confusing for me, if I do this:
items = Item.buildItems()
I get:
TypeError: buildItems() missing 1 required positional argument: 'self'
It was my understanding that 'self' was always implied. If I do this:
items = Item.buildItems(self)
it returns an "error 'self' is undefined"
obviously.
I'm just very confused, I can't seem to figure out what is different about this example and when I was learning about classes originally.
Any help guiding me through this would be greatly appreciated, I feel like it would unlock a lot conceptually for me.