Could you help interpre the code? Thank you so so so much!

Hi, I have a difficulty to understand this chapter. I just follow the instructions to write the code but don’t understand how it work and what does it do at all, Can someone help explain in a super-easy way? So basically, we create various methods( I take it as function, object ) in one class ( class is like a big basket with different fruits, object or I should say methods are the fruit). is it correct?

below code, why we put self.calc_length() inside the def init ?
below code, what does that mean self.length = length inside the def calc_length?

Thank you so much in advance!

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,data):
    self.data=self.data+[data]
    self.calc_length()

fibonacci = NewList([1, 1, 2, 3, 5])
print(fibonacci.length)

fibonacci.append(8)
print(fibonacci.length)

Hi @candiceliu93:

These are concepts in OOP in Python. __init__ is a constructor, which allows you to initialise instances of the NewList class (like a Cookie Factory creating cookies). calc_length and append are methods in the class NewList. Methods are basically functions in classes and allow some action to be performed on all instances of the same class (like the shape of the resultant cookie).

Thus, the method calc_length is called in __init__ as it is one of the methods the instance should have (e.g. length of the cookie should be defined for each cookies made in the factory).
self.length = length is basically setting the length property of the object instance, which is to be returned to the __init__ constructor, which builds the object with 2 properties length and data. (A worker should be able to make each individual cookie given its shape and length, even if these properties may be different from one cookie to another)

Hope this helps!

1 Like
def  __init__ (self, initial_state):
self.data = initial_state
self.calc_length()

You instantiate your class with this. When you call a class object, you can have several instances of that class. You get several instances when you change the initial_state . The initial state for this example is [1, 1, 2, 3, 5] but it can be something else. fibonacci = NewList([1, 1, 2, 3, 5]) is just one instance of the NewList class.

When you see self inside a class, you want to call and use this variable or function inside or outside the class. For example self.data = initial_state used inside the NewList and self.length used outside the class.

When you call a class, the __init__ method is the first to run. Then it stops. The __init__ method runs when you call this fibonacci = NewList([1, 1, 2, 3, 5]). It stops. You have to call another of the class method. For example cal_length like this fibonacci.cal_length(). However, we jumped this step by putting self.calc_length() in the __init__ method. So after running __init__, the self.calc_length() calls the cal_length() method and runs it. It is quite convenient to do so here because this method does not take any input outside the class. Unlike the append method.

We use the self.length because we want call it inside the NewList class and outside the class. In this case, it wasn’t used inside the class. But we use it outside the class as fibonacci.length to see the length before and after appending.

When you run the fibonacci.append() method, it takes the initial_state as self.data, adds the new value to self.data . Then self.calc_length() calls the length method to recalculate the length. So you calculate the length when you initialize and you recalculate the length after append. This is why you have two print(fibonacci.length) with different values.

self.data=self.data+[data] is what updates the self.data by assigning it to itself. This updates the self.data in the __init__ method.

1 Like

Thank you for your explanation! need time to digest it!!!

1 Like