For the past three weeks of the course, we've been focusing on OOP, more specifically creating classes and writing methods for them. Even though we did briefly cover classes in CSC108, they were explained with much more detail in CSC148 In addition to this I think that revisiting the concept the concept after a break helped me understand it better.
One new concept that was introduced from the very start of 148 was the idea of creating classes and methods from a paragraph of plain English, instead of being instructed to 'create the class ____ with the following methods...etc'. Even though this practise seemed quite easy during lecture it became more difficult during the labs where the class names were based on non-nouns.
For my own future reference, lets dive into what was taught about OOP:
First of all, what is an object? Essentially an object is any sort of entity having an identity, type, and value (which may include some number of attributes). OOP is centred around the fact that programmers can create/define classes of objects so that any object of that class is characterized by a defined set of attributes.
To help me explain better, consider this piece of code for the class Point:
The __init__ method is a special method that creates/initializes the class when you create it. The first argument of the __init__ method is always self (it could be some other random name, since it would still serve the same function, but the programming community would probably have you hanged deeply frown at you for doing so). Self is the argument of the class that refers to itself. For example, consider the __str__ method. The method is supposed to return the 'point' in the notation that we usually see in math class:
Notice that the method __str__ only takes the argument self'; this is because the __str__ method 'is applied' to only the object itself.
The class Point has two instance variables (variables that are defined inside a method and only belong to one instance of a class), x, and y. These two class variables can also be used in any method created within the class. The lines self.x = x and self.y = y simply say that each object of the class Point has a property x and a property y which are defined as the first and second variables when an instance of the class is created in the shell (ex: p = Point(2,3) where x = 2 and y = 3)
In the second week of class, we were also briefly introduced to Abstract Data Types (ADTs), some of which (lists and dicts) we were already quite familiar with. We were introduced to the class of Stack. Stacks are essentially the same as lists, however the user can only access/manipulate the most recently added item.
OOP adds a lot of flexibility to how we can manipulate objects in Python, and so far it isn't too mind blowing, however I'm apprehensive of applications of classes where I won't understand exactly what the class is supposed to be (ie how to start working on a problem). However that issue heavily depends on the problem itself so I'll just have to wait and see.


No comments:
Post a Comment