Wednesday, 25 February 2015

WEEK 7: Debrief and Object-Oriented Programming (OOP)



     Success!

     Even a small challenge to overcome is a big step for a beginner programmer like me. I practiced recursion over the break and I finally got the hang of it! It may have taken many, many trials but I can say with some confidence that I understand recursion to some extent now. This week we are going to add on to what we already know using Binary Trees and practice recursion a tad bit more. It always feels like I've fallen behind when I don't finish my lab but I'm making an effort to completing it before the next lab.

     Now on to this week's topic: OOP

     I chose to write about OOP instead of recursion because I want to take this opportunity to step back and analyze for myself exactly what it is about OOP that I know and also to review the aspects of it I didn't fully understand weeks ago (when doing the first assignment). What is an object? What does it mean to be object oriented? These were the first questions I asked on the first day of classes. Here's what I know now:

     In python, all values are individual objects held a specific memory address that stores information and behavior (type of value and operations for it) of that object. So when we call an object we are calling onto the content (information/value) that it's memory address holds (unless directed otherwise).

    Within this type of programming we can also create a new class of objects. By creating a new class we can create any type of objects that operate in the way we design it to (with it's own functions, methods and restrictions). Here's an example of how a class is created:

    class ClassName:

           """This creates a new class. Your description of the class goes here."""

           def __init__(self):
                 """ (ClassName, type) -> NoneType
                 This method initializes ClassName with it's required parameters.
                 """
                 # initialize any parameters here.

           def specific_class_function(self):
                 """ (ClassName) -> Value
                  This method uses the argument to return something.
                  """
                  # implement function here based on intended return value and parameters.

use __str__ to create a string method representative of ClassName

use __repr__ to create an official string method representative of ClassName

use  __eq__ to create a method that compares two ClassName objects to see if they are equivalent.

Subclasses are child class of the parent class it takes as it's argument. A subclass inherits everything from the parent class and can also override the functions defined in the parent class.

    class SubclassName(ClassName):

           """This creates a subclass of ClassName. It inherits everything from ClassName."""

           def __init__(self):
                 """ (ClassName) -> NoneType
                  This method initializes SubclassName with it's required parameters.
                  """

Creating new objects can be simple or complicated as you want it to be and this is what makes OOP subjective and useful. The more we learn about OOP the more options we have at our disposal.

Cheers

(If there is any conceptual errors please let me know!)

1 comment:

  1. The definition of an object is more abstract than the way it is stored in memory or implemented in Python. Object oriented programming is a general programming paradigm. "Objects" are just an abstraction; a way to think about collections of information and processes within a program so that they correspond to our intuitive understanding of the real world. Classes are clearly-defined "kinds" of objects: literally a class of objects (as in "classification").

    In Python, everything is an object; but that's not the case for all OOP languages.

    ReplyDelete

I'm open to any feedback! Thanks!