Fixing my code

Thank you very much for all previous posts, I find them very helpful.
So, I have combined them into the following code, please suggest if we can make more improvements…

Special thanks to patrickd314, we updated:

class SortedList(list):

  def __init__(self, lst):
    super().__init__(lst)
    self.sort()

  def append(self, value):
    super().append(value)
    self.sort()

  def __str__(self):
    return '''__str__ is giving us more output control! 
Our output is: ''' + repr(self)
    
list_object = SortedList([4, 1, 5])
print(list_object)
# __str__ is giving us more output control!
# Our output is: [1, 4, 5]
list_object.append(2)
print(list_object)
# __str__ is giving us more output control!
# Our output is: [1, 2, 4, 5]
list_object.append(-1)
print(list_object)
# __str__ is giving us more output control!
# Our output is: [-1, 1, 2, 4, 5]

class SuperDict(dict):
    
  def __getitem__(self, key):
      try:
        return "The value for key {key} is {value}!".format(key = key, value = super().__getitem__(key)) 
      except KeyError:
        return "This key does not exist in this dictionary!"
    
dic = {i : i**2 for i in range(1, 6)}
super_dic_object = SuperDict(dic)
print(super_dic_object)
# {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
print(super_dic_object[2])
# The value for key 2 is 4!
print(super_dic_object[6])
# This key does not exist in this dictionary!
1 Like

It’s customary to put the __init__() method first; otherwise, :slightly_smiling_face: .

Also, we like to control the output of a call to print(obj) by means of a __repr__() or __str__() function:

  def __str__(self):   # or __repr__(self)
    output = ""
    for item in self:
        output += str(item)
    return output

(Just return self won’t work, as self is a SortedList object, not a string.)

1 Like

patrickd314,
I tried to implement your suggestions.
Could you review our changes please?

That part needs a slight edit of the quotes:

  def __str__(self):
    return '''__str__ is giving us more output control! 
Our output is: {}'''.format(repr(self))

Then, for the following calls:

my_list = SortedList([4,2,8,1,5])
my_list.append(-1)
print(my_list)

… you see this printed to the screen:

__str__ is giving us more output control! 
Our output is: [-1, 1, 2, 4, 5, 8]

Remember, that you can also override the behavior of repr() by means of __repr__(), the general idea being that __str__() is supposed to give you “pretty print” output for the benefit of your user, while __repr__() should contain more detailed, “under-the-hood” information for your benefit, as the developer.

The “string” output of repr() is simply Python’s default. You can make it what you want. You could have a list of all the appends, for example, if you needed it.

2 Likes

Are you referring to repr()? Yes, that is the default behavior, to return a string representation of an object.

My point was that, if you create a class, then you have the ability (or even responsibility) to determine what a “string representation” of your object looks like.

1 Like