PYTHON OBJECT-ORIENTED PROGRAMMING

Object-Oriented programming is an essential skill for any serious Python programmer. It is one of the most important programming paradigms, and vast amounts of code exist which make use of it.

GETTING STARTED WITH OBJECT-ORIENTED PROGRAMMING IN PYTHON

The best way to learn about object-oriented programming is to dive right in and write some code using it. All the terminology and concepts will make much more sense when you’ve had just a little experience coding with OOP. Then we can come back and explain the whys and wherefores, once you have actually written some Python OOP code. So, spin up your favourite Python editor, type in the code below and then run it.

class Card:
    def __init__(self, value, suit):
        self.value = value
        self.suit = suit

    def __repr__(self):
        return f"{self.value} of {self.suit}"


if __name__ == "__main__":
    card1 = Card("Ace", "Spades")
    card2 = Card("Queen", "Hearts")

    print(card1)
    print(card2)

How did you get on? If you did this correctly, you should have got the following output:

Ace of Spades
Queen of Hearts
  • Think about what the code did and try to figure out how it created the result that it did.
  • Make a copy of the original file you created and in the copy, try changing some things.
  • Try creating some new cards, in the same way you created card1 and card2.
  • Don’t be afraid to break the code, that’s why you made a copy. Break it, try and fix it, try and extend it. Basically make it your own and don’t be afraid to experiment.

If you are phased by if __name__ == "__main__": don’t worry. It’s just a standard Python convention for running code only if the file is the main entry point for a project. If it’s scary at this stage, you can just leave it out, and un-indent everything beneath it one level.

Congratulations. If you are new to OOP, you have just written your first object-oriented program in Python!

OBJECT-ORIENTED PROGRAMMING WITH PYTHON TERMINOLOGY

You are now no longer a complete stranger to OOP with Python. Well done! In this section we are going to cover some key terminology associated with object-oriented programming. It is important to understand these terms so you can reason clearly about how OOP code works.

A Class

Think of a class a a template. There are several real-world examples you could think of for comparison. For example a cookie-cutter, or the blueprints for a particular type of aeroplane such as a Boeing 747.

An Object

There is a key distinction between a class and an object. If the class were a cookie-cutter, an object made from that class would be an individual cookie. The same applies to the Card class we looked at. Each individual card (like card1 and card2) is an object made using the class.

Instance/Instantiation

Another word for an object is an instance. So card1 and card2 are instances of the Card class. Instantiation means “to create an instance of.” I.e. to create an object from the Class.

A Property/Attribute

A Property or attribute is a data value associated with an object. card1 has a value for its suit property of "Spades". card2 has a value for its value property of "Queen".

A Method

A method defines the available behaviours of objects created from a particular class. In our example, there are two methods : __init()__ and __repr__(). You don’t need to fully understand what these do yet. Just recognise that they are functions defined inside a class. This is a useful shortcut for thinking about what a method is: a function defined inside a class.

The Constructor

The constructor is the function which gets called when you create objects from your class using code like card1 = Card("Ace", "Spades"). In Python, the constructor method is called __init__(). In our cards example this is where the arguments that got passed in got assigned as properties of the object being created.

Self

self in Python OOP can be confusing for beginners to understand. It may seem a bit redundant to write self.value = value. However what this does is assign value to the value property of this particular instance of the class currently being created. I.e. the object being created.

ACTIVITIES

  • Read through the definitions above several times. Try your best to understand them, and refer back to the code you wrote in the last section to help give you context
  • Once you are confident that you understand a particular term, explain it to someone else in your own words

You need to eventually really digest these definitions. Once you have a good grasp of the terminology, working with OOP will be much easier. Don’t worry too much if things are still a little hazy at this stage, as with experience you will become more familiar and confident with the language of object-oriented programming.

PYTHON OBJECT-ORIENTED PROGRAMMING TEACHING PACK

Are you a teacher?

Check out this complete unit covering the concepts of object-oriented programming and their and application to Python programming.

PYTHON OBJECT-ORIENTED PROGRAMMING Teaching Pack

PACK CONTENTS

  • Slide-show introducing Object-oriented programming and the associated terminology
  • 7 Student worksheets for working with object-oriented programming in Python, including coding exercises and extension activities
  • Python object-oriented programming quiz
  • Python solutions for programming activities as .py files

PYTHON OOP EBOOK

Maybe you would like to learn more about OOP from an EBook, with source code provided for all examples.

Python OOP Ebook

  • Understanding concepts and terminology related to object-oriented programming
  • Working with properties and methods of objects
  • Encapsulation, getters and setters
  • Turtle Graphics and object-oriented programming in Python
  • Class inheritance
  • Polymorphism

This article has given a brief introduction to object-oriented programming with Python. OOP is a big topic and there is a lot to learn. You will need patience and a lot of practice to get good at it, but hopefully what you’ve read here will has given you a good start and piqued your interest.

Happy computing!

Sharing is caring!

Leave a Reply

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