Monday, February 23, 2015

Object Oriented Programming Concept Summary

So far, we have learned how to create classes, subclasses, methods, and how to implement these in the proper style for both readability and efficiency. But how does this relate to object oriented programming? While we're at it, what is object oriented programming? Well to answer that, we must first define what we mean by an 'object' in the context of computer programming.

An object is essentially a chunk of data; ones and zeros grouped together, represented in a way that humans can understand. For example, a variable in python: NUMBER = 2. As we can see, the variable refers to the integer 2. It also refers to a memory address in the computer, since the variable and the value it represents must be kept track of. It gets much more complicated than just simple variables, as complex classes containing multitudes of data types and information, which are all represented as a single object. At its lowest level, it is just a series of incomprehensible data, but object oriented programming groups it into something that humans can understand, and from there data can be manipulated.

Speaking of manipulating data, this is what functions are for. They might not necessarily manipulate data, but they perform an action with the data. It could represent the data in a certain way, or it could write the data to a file. An object class is a group of functions called methods. The class can be used as an object, and the methods within the class determine how it stores and manipulates data. Typically, there is an __init__ method, which is short for 'initialize,' as that is exactly what the method does. It performs certain actions that allow the object to work. Usually included are a __str__ method (returns a string representation of the object, meant to be readable by the user) and a __repr__ method (a representation that is unambiguous, a raw representation of the data for the use of the programmer). Finally, there might be an __eq__ method, which just compares two objects of the same type. Not all of these methods are implemented in every class. Sometimes it wouldn't be applicable to give a string representation of an object, or perhaps the method is implemented in a subclass.

A subclass is a class that inherits its base features from another class. For example, one would make a generic class to keep track of names, and implement a subclass for the names organized by first name, and one for the names organized by last name. Having generic classes makes the code easier to read, and allows the code to be reused in the future if more subclasses are to be implemented.

Through the use of class objects, very complex programs can be constructed, from video games to integral calculators. Or a video game where the user solves integrals, or an AI that uses integrals to beat video games, or a program that calculates the... okay that's enough, I hope you've enjoyed this summary of object oriented programming in python.

No comments:

Post a Comment