Ad
  • Default User Avatar

    Well, the description clearly states 'you should NOT edit the item constructor/class'. Did you survived the goblin attack?

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    Last but not least the collection object is injected in your class by reference. This means that other code outside your class can mutate the object by adding/deleting elements. If you counted the elements only once at construction time your result is now out-of-date since somebody else mutated the object which your self.collection points to. So in this case you give false results. To make sure that you always give back the correct result your recalculate len since the state of your collection may have changed. Consider the example below

    a = [1,2] #someone else creates the object
    obj = PaginationHelper(a, 1) #injects the object to your class
    print obj.item_count()#prints 2
    a.append(3) #someone else mutates the object
    print obj.item_count() #prints 3, you are still up-to-date remember you hold a reference to the original object you don't have your own copy
    
  • Default User Avatar

    For the calculation of the len(self.collection) you could be right in this particular example since we never add items to the collection. For a real life scenario we would like to add elements to the collection as well and this would invalidate the number that you calculated in the init. You could use properties to update your calculated lengh variable with every mutation of the collection object and always return the up-to-date calculated value.

    For the memory usage you are almost wrong. The collection object is a list which is a mutable object in Python. As a result pass-by-reference is used in the init thus the collection is not copied in the class state. But as I said above in this particular kata we don't add anything to the collection so you could argue that you don't need to save it.