The midterms were alright, but I know I am still a little unclear on how to do object-oriented programming.
This lack of understanding is reflected on my midterms. The marks I lost were all due to the fact that I did not know what to include as my parameters under the __init__ method.
Here, it states that when creating the class Point, we can include x and y in the __init__ method to create a more general class.
I'm not sure what they mean by general, but I suppose it means that by setting x = 0 and y = 0 in the __init__ method, we no longer HAVE to include a parameter for x and y. If we want to change the value of x and y, we simply include it when we initialize a an object Point
Otherwise, if we had not included x and y in the __init__ method, you would have to create a point with another line of code each time, for example,
p = Point()
p.x = 3
p.y = 4
as opposed to just having p = Point(3, 4)
From my understanding, depending on what you want your program to do, you set the parameters of the __init__ method based on how much you will be using them. For example, with point, if the goal of the program is to find the midpoint and the distance from one point to the other, both functions need to utilize a point in space, which is why we need to initialize a point x and y each time.
After my attempt to explain object-oriented programming in my own words, I think I have grasped a better understanding of it.
...This is why geeks should write.
No comments:
Post a Comment