Python language basics 26: objects

Introduction

In the previous post we looked at how to comment your Python code so that it becomes more understandable. We saw a couple of techniques to write comments and document your code.

In this post we’ll discuss the probably most important ingredient of object-oriented programming languages: objects.

Objects

Everything is an object in Python. Store this information away in your mind as you’ll probably hear it quite often as you learn Python. In order to understand objects we’ll need to investigate data types called reference types.

When you assign an integer – or in fact any type – to a variable like this…

x = 5

…then the following things happen in a very simplified way:

  • The variable ‘x’ is put in a place in memory called the stack
  • The value “5” is put in a place in memory called the heap
  • “5” will be referenced by “x” through a pointer

So ‘x’ is really only a facade or a container for the value of 5. The name of the facade is “x” and it points to the value “5”.

All variable assignments are handled like that in Python. All variables are reference types. Therefore they shouldn’t even be called “variables” as that term is more reserved to the so-called value types. Value types are only stored in the stack and can be found in other popular languages such as C# or C++. In those languages primitive types such as integers or characters are value types. They are not referenced, they are stored in a single space in memory. However, the term “variable” is still prevalent in Python so we’ll continue to use it here as well.

Python, in contrast has no value or primitive types. All types are reference types.

What does all that mean for a programmer? Consider the following:

x = 5
x = 10

x will first get the value of 5 and then 10 immediately after that. At first ‘x’ was pointing to the value of 5 on the heap. Then a new value, 10, was created in memory and the pointer of x was redirected to the new value. What happened to 5? It’s still there on the heap but as it is not referenced by any variable – or object – it is marked for deletion for the garbage collector (GC). GC is a mechanism to manage the available space in memory and remove all unused values, i.e. to free up the space occupied by values with no references.

Let’s take another example:

x = 5
y = 5

Now both ‘x’ and ‘y’ will point to the same value ‘5’. ‘5’ will only be created once on the heap, not twice. X and y are both integer objects and objects are reference types, therefore x and y are also reference types.

In fact a variable assignment is similar to the dictionary type where an entry has a key and a value. In a Python variable assignment the key is the name of the variable and the value is the value assigned to that key.

In the next post we’ll look at the basics of value and reference equality in Python. The notion of equality is strongly tied to objects.

Read all Python-related posts on this blog here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: