Python is an object-oriented language. The code in this book, however, is targeted at programmers from all walks of life, including those who aren’t used to objects—so I avoided object-oriented programming in most of this book. There are only a few examples where I used object-oriented programming, because some library required it. This short section tells you everything you need to understand those examples.
You can see a Python object as a special kind of variable. Regular variables belong to the language’s built-in types, such as float or bool. By contrast, objects belong to types that are defined by yourself, or by a library. These higher-level types are called classes.
For example, Python has no built-in type for dates—but the standard library datetime defines a class named date. Import that library, and you can create date objects:
| from datetime import date |
| moon_landing = date(1969, 7, 20) |
Running these two lines creates a variable called moon_landing that contains a date. To create that date, we passed it a year, a month, and a day.
In some other languages, you use the keyword new to create an object. In Python, you just use the name of the class, followed by parentheses. You pass the object creation parameters inside the parentheses, just like you would for a regular function call.
Once you have an object, you can call its methods. Methods are like functions that are specific to the object’s class. For example, dates have a weekday method that returns the day of the week, from 0 to 6:
| moon_landing.weekday() # => 6 |
Now we know that the moon landing happened on a Sunday.
Just like functions, some methods take arguments. For example, the date class has a replace method that returns a copy of the date with a different year, month, or day. It also has a format method that formats the data according to a format string. Here’s a snippet of code that uses those two methods:
| viking_1_mars_landing = moon_landing.replace(year=1976) |
| viking_1_mars_landing.strftime("%d/%m/%y") # => 20/07/76 |
Also like functions, methods can have default arguments, and can be called with named arguments.
For historical reasons, the name of the date class is all lowercase—but today it’s customary to use camel case for class names, as in ThisIsAClass. For example, the Keras library that we use in Part III of this book defines classes with names like Sequential, BatchNormalization, and MaxPooling2D.
In some languages, such Java and C#, you cannot even write a basic program without classes and objects. By contrast, Python’s object-oriented features are almost optional. You can easily write a procedural Python program without any objects or classes… Or at least, that’s until you take a deeper look, and you find out that objects and classes are woven right into Python’s fabric. For example, strings are actually objects with their own methods:
| 'strings are objects'.upper() # => 'STRINGS ARE OBJECTS' |
Even if objects and classes are core to Python, however, you can go pretty far without paying them much attention. In particular, we use objects in a very limited fashion in this book. We don’t define our own classes—we just import classes from libraries such as Keras. We use those classes to create objects, and we call those objects’ methods. That’s pretty much all the object-oriented programming you need here.