Salvatore Lopiparo

Software Engineer

Super Simple Python – Objects

You may have heard of the term Object Oriented Programming (OOP) when hearing about some programming languages, such as Python, Java or C++. In this lesson, I’m going to explain what OOP means, what it really means, and what it REALLY really means.

OOP – What it means

Nearly everything in Python (with similar concepts in Java, C++, etc.) is derived from object, the base class for everything. A string, an integer, a dictionary, a function, a class; all are sub-classes of object.

What is object you ask? Well, it’s a thing. Very similarly to how a Widget is an abstract idea of some product in Economics class – an object is the abstract idea of some programming thing.

OOP – What it really means

An object by itself is fairly useless until you do something with it. Lists, which are made from objects, are very useful because they have extra stuff attached that make it unique from other objects. The same goes for strings, dictionaries, and numbers. This extra stuff comes in two types: methods and attributes.

Methods are functions attached to an object. They usually do something with the object or to the object that they are attached to. For example, a list is an object:

>>> my_list = [3, 1, 2]
>>> my_list
[3, 1, 2]

This list can be sorted with a call to a method called sort.

>>> my_list.sort()
>>> my_list
[1, 2, 3]

The method sort is a function that sorts the list that it is attached to. Any function attached to an object in this way is called a method. Here’s some other examples we’ve already seen:

>>> my_list.index(2) # index() is a method
>>> my_dictionary = {'bananas': 34, 'apples': 27}
>>> my_dictionary.get('bananas') # get() is a method on dictionaries

Attributes are variables attached to an object. Attributes work just like methods, except while methods hold a function to be called, attributes hold some data for that object.

We’ll talk more about attributes and their uses when we discuss classes in another lesson.

OOP – What it REALLY really means

Because everything is an object, we can abstract away difficult concepts by compartmentalizing our code. Anything that has to do with dogs goes on the Dog object; say dog.make_noise(), which barks. Anything that has to do with cats goes on the Cat object; say cat.make_noise(), which meows. That allows us to then call my_pet.make_noise() without knowing what kind of pet we have. That is the true power of OOP.

Don’t get discouraged if Object Oriented doesn’t make much sense yet. It is one of the most difficult concepts to grasp in programming. Having a Computer Science minor, I didn’t truly understand it until several months of working as a programmer. If you’re interested in a more in-depth study on abstraction of objects, check out Code Complete by Steve McConnel.

Next lesson, we’ll do some practice with objects and abstraction by creating our own class! Until next time!


Your email address will not be published. Required fields are marked *