After we have learned Pythons basic syntax by using it as a calculator, we want to have a closer look at the concept of objects in Python.
As a starting point we want to get a basic understand of objects in the meaning of computer sciences. The concept and usage of objects in programming languages is connected to the object-oriented programming paradigm. In this understanding objects are representations of specific things of the real world. Objects translate things together with the associated logic, the context, and the interactions between these things in the digital space. This paradigm was very successful in the past as it empowers developers not only to avoid code replications but also to provide logical meanings of things inside of programming languages. As this concept may sound confusing at first, we want to explain it based on a concrete example.
Imagine you would like to translate the functionality of a bicycle to a digital space, so that a computer could understand the mechanisms and the properties of a bicycle. Before we start with the translation, we must think in an object-oriented perspective. In principle out of an object view everything can be differentiated in features and characteristics. The characteristics are specific properties that make a bike (or more general a thing) unique so that we can differentiate it from others, whereas features are functionality, that can be done with the bicycle. In this perspective we could think of the following characteristics and features:
Note: Of course, these lists of characteristics and features do not cover every possible property and functionality of bicycles in the real world. Although this should never be the intention or the aim of an object or a model in general you could try to think of other characteristics and features that you would like to add. In general, it is most important to translate the characteristics and the features of a thing that are meaningful for your programming purposes.
As we can see, the characteristics describe the properties of a bike or more general of a thing. These characteristics allow us to identify a specific bike and differentiate it from others. For example we could think of two different bikes:
Property | Peter's Bike | Sarah's Bike | |
---|---|---|---|
Colour | Black | Red | |
Owner | Peter | Sarah | |
Year of manufacture | 2004 | 2016 | |
Brand | No Name | Stevens | |
Kind of bike | Trekking bike | Racing bicycle | |
Size of bicycle frame | 28 | 26 |
Based on this table we can see that properties only store information. We could describe the kind of information based on the knowledge that we have already learned:
Property | Peter's Bike | Sarah's Bike | kind of information | |
---|---|---|---|---|
Colour | Black | Red | string | |
Owner | Peter | Sarah | string | |
Year of manufacture | 2004 | 2016 | number | |
Brand | No Name | Stevens | string | |
Kind of bike | Trekking bike | Racing bicycle | string | |
Size of bicycle frame | 28 | 26 | number |
Note: You see that object properties in a programming perspective are nothing more than value container (variables) that are linked to a thing.
Note: In the object-oriented paradigm characteristics of things are named as object properties.
To get a specific property of an object in Python we have to use the .
. Assume that there are two different bike objects in your workspace that represent the bike of Peter and of Sarah. The names of these objects are:
Peters_bike
Sarahs_bike
To access a specific property of an object we need to use the following syntax:
<object_name>.<property_name>
That means if we want to know the brand of peters bike, we could access this property by:
Peters_bike.brand
Note: Because object properties are nothing more than variables the access works also just with the name. No brackets are needed.
In contrast to object properties object methods are the programmatic representation of the features of a thing. As we have already learned features are mechanisms that can be done with a thing. In this understand they are closely related to the concept of functions that we've got to know in the previous chapters. A method of an object is called by:
<object_name>.<feature_name>()
Note: The call of an object methods is done with brackets
()
. That make sense because object methods are functions that are related to an object.
If you want to apply the brakes of Sarahs bicycle you do this by:
Sarahs_bike.apply_brakes()
We do not want to deepen the concept of objects any further in this tutorial. If you are interested in this concept, there are a lot of great books and online tutorials that explain how objects are build and programmed in Python exactly. For the purposes of this tutorial, it is enough to have a basic understanding of objects and their usage. In Python everything is an object. Therefore it is important that you can differentiate between object properties and object methods to make use of Python's objects properly.
Citation
The E-Learning project SOGA-Py was developed at the Department of Earth Sciences by Annette Rudolph, Joachim Krois and Kai Hartmann. You can reach us via mail by soga[at]zedat.fu-berlin.de.
Please cite as follow: Rudolph, A., Krois, J., Hartmann, K. (2023): Statistics and Geodata Analysis using Python (SOGA-Py). Department of Earth Sciences, Freie Universitaet Berlin.