I have a class that should act as a list of differences (deltas), as calculated from another list. For example, if I have a list of positions, I can use my class to have a dynamically created list of displacements.
For that, I’ve used the __getitem__ method the following way:
class Deltas(object):
def __init__(self, ref):
self.ref = ref
def __getitem__(self, index):
if index == 0:
return 0
else:
return self.ref[index] - self.ref[index-1]
samplelist = [1,2,3,5,7,9]
deltas = Deltas(samplelist)
If I print individual elements, it works fine, but if I print the whole list, it gives me:
> print deltas
<__main__.Deltas object at 0x7f7aa93e3b50>
I’d like to know what other method I have to implement to get this:
> print deltas
[0, 1, 1, 2, 2, 2]
For example, adding this method would work:
Or, alternatively: