Skip to content Skip to sidebar Skip to footer

Class And Object Referencing In Python, Not Understanding How Values Are Returned

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

Solution 1:

There are several things to consider here:

  1. your function buildItemsis not part of your Item class. That means calling Item.buildItems() doenst work. This should give the error AttributeError: type object 'Item' has no attribute 'buildItems'. Your provided error message is probably due to the fact that the indentation is actually different?

  2. 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>'
    
  3. 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:

    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 __repr__(self): 
              return str([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):
     return self.__str__()

if you want your output to be as you showed in your question to look like a list

 def __repr__(self):
     return str([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 in range(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"