Sunday, February 15, 2015

OOP

    At first I thought we are supposed to write about OOP after the reading eek, cause it didn't look like something we have already covered. Then I realized it is due before reading week. Looked it up and found out this is pretty much what we were doing throughout this whole course.
    Will start with Class first, because it's kind of hard to explain other concepts without it. Class allows us to create an object with certain set  of attributes, which are of the same(or very simmilar) types for all objects of one class.
    Inheritance lets us create subclasses (which are based on bigger classes called superclasses). They are needed in order to make code more flexible and easier to edit in the future without applying the same changes again and again for every little class we need. Instead we can just change superclass, and all the generic attributes inherited from it by subclasses will be changed as well. For example creating a superclass Shape which has one attribute which all shapes are going to use, regardless of what the actual shapes are, their sizes, colors etc. That is position. And then create subclasses of shape for example Square or Triangle which will have different amount of sides, hence won't be able to inherit this attribute from parent class.
    Methods do the same thing as functions do, except for they are attached to classes while functions are written in the module. We can use methods to create and change(aka mutate) characteristics of an object. If we take class Square from previous example, we would might want to write a method change_base which would change the length of base of square already created. There are 4 methods which have to be included in most classes: __init__,  __str__,  __repr__,  __eq__.
   __init__ allows you to create an instance of an object
    __str__ gives a string representation of an object description understandable for user
    __repr__ provides information on an instance of a class which is precise enough to create an exact copy of an object
    __eq__  lets us check if two objects are the same, based on attributes we want to check rather than just checking where the object is stored in memory.

Sunday, February 8, 2015

Week 5. Tracing recursion impressions

    Tracing recursion calls is a task which requires a lot of concentration and can be tedious at first, but with some practice it becomes a lot more understandable and easier. First thing I thought was "why does this keep happening? There is no loop! Whyyyy?" And then "How does it know when to stop?" and then "I am dumb.". Obviously after couple of traces and attempts to write some recursive functions it became clear that a loop is absolutely unnecessary, since these functions call themselves, and have an if statement in them, so they kind of already are loops on their own.
    Knowing about this concept would have saved some time and effort in previous assignments in 108 and school, but it's understandable why nobody teaches it right away. It is hard to understand if you are not familiar and are able to use other concepts. Even now it is a bit hard, even though quick sort demonstration during the class helped with understanding of actually writing recursive functions a bit.
    Lab 5 handout looks like we are going to write some recursion functions, which is a bit scary, since I still didn't manage to make most of the ones I was practicing on work properly, but isn't it why we all are doing this? For that feeling in the end when you manage to make things work?