It's been 3 weeks since the Winter term started and CSC148 has been enjoyable so far. Basically up until now we have been continuing from 108 on objects and classes. In the first week we started with a neat Python program called Turtle. So essentially what it does is that it draws a "Turtle" which looks like an arrow on the screen and then you can begin to move and create arms for the Turtle. This was really interesting because all we've done so far are text based programs and functions but nothing visual.
So the point of the Turtle program was just to show objects and classes. First in the Turtle class we created a turtle object. Along with this object came with some properties that were established in the __init__ method such as self.arms = n which creates arms for the object, where n is the number of arms. In this class also contained other methods such as move which would move the turtle around the screen.
We also covered over some other special methods or built-in methods in classes such as __str__ and __eq__ and that we can modify them. However, Professor Heap gave us a good warning to be careful not to reinvent the wheel.
Moving onto the second week, we got into some new stuff such as making variables and functions "private", new data types, and finished off with some doc-testing and unit testing. We talked about by just adding a "_" in front of variable will make the variable inaccessible to other programmers, but it doesn't prevent them from just importing the code and changing the variable themselves.
We also talked about ADT (Abstract Data Type), but really all we need to know about them is how we call them and what we get back when we call them. Then we looked at doc-testing where it tests through each example in your docstrings and unit test where it runs a file that has bunch of tests that are used to test another file.
Finally, up to week 3 we moved into talking about sub-classes and inheritance. So basically what it is, is that we can essentially expand a class with other classes. Why do we want to this you ask? Well really sub-classes and inheritance is not absolutely mandatory in order for your program to work, but what the purpose of using inheritance is to organize and minimize code. In Python, we can extend a class by adding in new functions and variables that the parent class did not have, and we can also override the parent class by overwriting functions in the parent class in the new class. It is always better to extend or override code then copy and paste it into a new class.
Speaking of inheritance, sometimes in the parent class, functions requires a sub-class in order to run. For example in lab 2, we had to create a class that either took in a str or an int as one of the parameters. But it's not reasonable to have that class handle both at the same time, so we created two new sub-classes, one to handle the str and one to handle the int. So in the parent class for that function, we put in a "raise NotImplementedError("Error! Need a sub-class")" to alert the user that this function is not implemented in this class but it is in the sub-classes.
So yeah, this has been CSC148 so far. We just finished assignment 1 and our first midterm is coming up. I hope that I'll pass! Thanks for reading!
I looked into Danny's lecture note to figure out what you're talking about when you mentioned Turtle, and it's bringing back some memories from high school Turing class where we had to draw stuff like houses.
ReplyDeleteI find your SLOG very informative and well out-lined; I shall keep a close tab on this one.