Python language basics 70: starting with classes
December 6, 2015 1 Comment
Introduction
In the previous post we extended the topic of comprehensions and looked at comprehension filters. We saw that it was really easy to extend the standard comprehension “formula” with an if-clause. The filter is then applied to every element in the iteration and the comprehension function only considers those elements that pass the boolean test.
In this post we’ll start looking at something completely different: classes. Classes are probably the most important building blocks of object oriented languages.
Classes
If you have a background in e.g. C#, Java or C++ then you’ll know what classes are. Classes are how you can define your own data types. A class is a model of that data type. What data types do we mean here? We mean complex data types that are built on top of other data types. Unclear? Let’s see an example.
Let’s say that you need to work with cars in the project you’re working on. E.g. you’re writing software for a car dealership which buys, sells and repairs cars. Those cars must be represented in software somehow. How can we describe a car? What are its properties? A real car has a lot of components and properties but let’s start out with a simple list:
- A set number of wheels, usually 4
- A make, such as Ford, Peugeot, BMW etc.
- The year when it was produced
- Amount of petrol consumed per 100 kilometers or miles
- A colour
That’s enough to start with. Our initial car model is comprised of those 5 properties:
- Number of wheels, data type: integer
- Make, data type: string
- Year, data type: integer
- Consumption, data type: double
- Colour, data type: string
In Python we don’t have to declare those data types but I felt it was important to list the types to make a point: a class is a complex data type. It’s called complex because it consists of one or more other data types, not because it’s necessarily complicated. Most classes out there are not complicated at all, they are mere containers of other data types.
Classes help you build just about any type of model in code you can think of. You can describe buildings, animals, people, countries, cities, and so on. You typically name your class accordingly: the Car class, the Animal class, the Person class etc. It would be futile to call a class “Radio” when you really describe a “TV”, right?
So what is the connection between a class and an object? After all Python, along with Java, C#, C++, VB.NET etc., is an object-oriented language. Shouldn’t it be called a class-oriented language?
You can think of a class as the blueprint for an object. Such blueprints exist in reality. Very often a building has a plan, a blueprint where the building is represented as a model. You can then take that blueprint and construct a building based on it. And then build one more building, and then another etc. The buildings are the actual objects. In the world of programming we say that an object is instantiated or constructed from a class. You can instantiate as many objects from a class as you wish – well, as long as the memory of the computer can hold them.
So keep this in mind: a class is the model – the blueprint – and the object is the “thing” that is built from that model. There’s only one model but there can be one or many objects created during the lifetime of your software.
We’ll keep exploring classes in the next post.
Read all Python-related posts on this blog here.
Pingback: Python language basics 71: continuing with classes | Dinesh Ram Kali.