"I am now going to try to teach you the beginnings of object-oriented programming, classes, and objects using what you already know about dictionaries and modules. My problem though is that object-oriented programming (aka OOP) is just plain weird. You have to simply struggle with this, try to understand what I say here, type in the code, and then in the next exercise I'll hammer it in."Now, this was pretty discouraging when I first read it, mainly because of the use of the word "try." In short, my initial impression of OOP was a rather negative one, that is until I started learning it.
What is Object-Oriented Programming?
Object oriented programming is a procedural programming paradigm that focuses on the creation of objects which consist of attributes that both describe the object and give it a function. To illustrate this in the real world: a rock is an object that has multiple attributes: it can be solid, it may be small or extraordinarily large. However, this rock also has functionality, you may use it as a tool to create a fire, a weapon, or maybe just to stack on top of other rocks as a game.Back to the programming world, OOP consist of classes (the object) and methods (the attributes). A class is a blueprint of whatever you want to create, in this blueprint you must create methods to give meaning, procedure, and functionality to your class. Surprisingly, you, programming in Python (and other OOP languages) live in a world of classes. Every Python method you use is a part of a class, like
str, int, float, and list. So, what's the big deal about OOP? The big deal is that it allows for great opportunities that allow a developer to increase the size and complexity of his program. OOP encourages creativity for developers to create new classes that can have real world application.
How do I Become Object-Oriented?
So we are going to make a Stack class that simulates the rock stacking game I mentioned earlier. (For sake of simplification, I am not going to consider the problems that have been mentioned in lecture as of Week 3. Also, I am going to insert picture of code instead of using the </code> tag with Blogger because I don't like how it looks.) I also am not going to be going over Python syntax, rather I will just explain the class in plain English so we can comprehend OOP.The Stack Class:
![]() |
So this is the blueprint for the class Stack which can be eventually used in the same manner
list or str is. What we can see is the following:When you create a Stack object, it initializes a new "Stack of rocks" by creating an empty list which _rocks refers to.
Now what I have done is added meaning and functionality to the class by giving Stack attributes:
- remove -- which removes a rock from the top of the stack.
- crumble -- which crumbles a Stack of rocks
- is_empty -- which checks if the Stack of rocks is empty.
- place -- of course we need a method that places a rock on the top of the stack.
Implementing the game:
Now let's see our class in action. What this code does is selects a random number from 0-10. This number will per-determine the time a rock Stack crumbles (if the number is 4, it will crumble on the 4th rock).
Next, we create the Stack and start to stack rocks on top of each other, keeping track of how many rocks have been placed using the count variable.
Once the number of rocks that have been placed in the stack is equal to the fall variable, the stack of rocks crumble and the game is over. Finally, you get a message telling you how high your stack was.
This is the output we get in one run of the game. Pretty cool eh?
Remember, you don't have to stick to the game. The great thing about classes is that you can do whatever you want with them! You can add more functionality, increase the complexity of the class, and increase its application to the real world.
Inheritance
The final thing I want to mention about OOP is inheritance. Inheritance is used to indicate that one class will get most or all of its features from a parent class.What I didn't mention above is what part inheritance plays in the Stack class.
When we create a class in Python, the class, by default, inherits the object class which includes a bunch of stuff, but most notably the built-in functions of Python. So declaring
Stack is the same as declaring Stack(object). If you look carefully, you can see that we have used the inherited methods from object; append and pop. If we wanted to, we could create another class that actually inherits from the Stack class like this: NewClass(Stack). NewClass will inherit all the attributes of Stack in the same manner Stack inherits from object. Actions on NewClass imply an action on Stack. When we do so, we have the ability to override the actions of the class by creating class with the same name. In doing this we have allowed for a new opportunity of complexity of our class. 

