Class And Object Referencing In Python, Not Understanding How Values Are Returned
Solution 1:
There are several things to consider here:
your function
buildItems
is not part of yourItem
class. That means callingItem.buildItems()
doenst work. This should give the errorAttributeError: type object 'Item' has no attribute 'buildItems'
. Your provided error message is probably due to the fact that the indentation is actually different?your function
__str__(self)
will be called if you actually convert your items into string as follow:myitems = buildItems() print(str(myitems[0])) # results in '<clock, 175.0, 10.0>'
However you are calling print to the list of objects and there the
__repr__(self)
function will be used to generate a string for each of your elements inside the list. Thus you need to override the__repr__(self)
function as follows:classItem(object): def__init__(self, n, v, w): self.name = n self.value = float(v) self.weight = float(w) defgetName(self): return self.name defgetValue(self): return self.value defgetWeight(self): return self.weight def__str__(self): return'<' + self.name + ', ' + str(self.value) + ', '\ + str(self.weight) + '>'def__repr__(self): returnstr([self.name, self.value,self.weight])
EDIT:
This would produce the output as you wanted. However this disagrees with the intention behind the __repr__
function as mentioned in the comment by @ShadowRanger. A better way to achieve an ouptut which could by copy pasted into the terminal is as follows:
def__repr__(self):
return self.__class__.__name__+ repr((self.name, self.value,self.weight))
`
Solution 2:
Your already on your way and have already defined a __str__(self)
method. This overrides the default object __str__
method. So any time you print the item it will print in the format you have defined, infact any time you try to use your class in a stringwise fashion it will call this method.
However when you have them in a list and print the list, the object represenation will call the __repr__
method. This is normally meant to be a representation of how to create this instance but can be anything you like it to be.
if you want to just return your same string representation you can just do
def__repr__(self):
returnself.__str__()
if you want your output to be as you showed in your question to look like a list
def__repr__(self):
returnstr([self.name, self.value, self.weight])
if you want to represent your class as it was constructed
def__repr__(self):
return self.__class__.__name__ + "(" + ",".join((self.name, str(self.value), str(self.weight))) + ")"
OUTPUTS
[<clock, 175.0, 10.0>, <painting, 90.0, 9.0>, <radio, 20.0, 4.0>, <vase, 50.0, 2.0>, <book, 10.0, 1.0>, <computer, 200.0, 20.0>][[clock,175.0,10.0], [painting,90.0,9.0], [radio,20.0,4.0], [vase,50.0,2.0], [book,10.0,1.0], [computer,200.0,20.0]]
[Item(clock,175.0,10.0), Item(painting,90.0,9.0), Item(radio,20.0,4.0), Item(vase,50.0,2.0), Item(book,10.0,1.0), Item(computer,200.0,20.0)]
So you need to override the __repr__
method but how it presents itself is really up to you.
Solution 3:
A section of your question ask's how to develop code like below
myitems = buildItems()
print(myitems)
to generate an output like below
[[clock,175,10],
[painting,90,9],
[radio,20,4],
[vase, 50, 2],
[book, 10, 1],
[computer, 200, 20]]
Below is a code snip that would generate similar output.
myitems = buildItems()
for i inrange(len(myitems)):
print(str(myitems[i]))
Output using __str__
define in the Item
object
<clock, 175.0, 10.0>
<painting, 90.0, 9.0>
<radio, 20.0, 4.0>
<vase, 50.0, 2.0>
<book, 10.0, 1.0>
<computer, 200.0, 20.0>
Reference:
Post a Comment for "Class And Object Referencing In Python, Not Understanding How Values Are Returned"