Screen Link:
https://app.dataquest.io/c/78/m/435/object-oriented-python/11/creating-and-updating-an-attribute
My Code:
class NewList(DQ):
"""
A Python list with some extras!
"""
def __init__(self, initial_state):
self.data = initial_state
self.calc_length()
def calc_length(self):
"""
A helper function to calculate the .length
attribute.
"""
length = 0
for item in self.data:
length += 1
self.length = length
def append(self, new_item):
"""
Append `new_item` to the NewList
"""
self.data = self.data + [new_item]
self.calc_length()
fibonacci = NewList([1, 1, 2, 3, 5])
print(fibonacci.calc_length)
fibonacci.append(8)
print(fibonacci.calc_length)
What I expected to happen:
I called calc_length method from the class and expected it to return the length of the list.
In the solution part, only length is called like fibonacci.lenght() am not able to understand if iwe haven’t defined any method with the name lenght how are we calling it here?
What actually happened:
<bound method NewList.calc_length of Class Name: <class '__main__.NewList'>
Class Attributes: {'data': [1, 1, 2, 3, 5], 'length': 5}
Class Methods: ['append', 'calc_length']
>
<bound method NewList.calc_length of Class Name: <class '__main__.NewList'>
Class Attributes: {'data': [1, 1, 2, 3, 5, 8], 'length': 6}
Class Methods: ['append', 'calc_length']
>