Python Friday #9: Classes (Part 1)

It took me a few weeks to reach classes and all the things I explained so far worked without them. This is a speciality of Python, then most other object-oriented languages use classes as the fundamental organisational unit for code.

This post is part of my journey to learn Python. You can find the other parts of this series here.

 

Classes

A class is a logical group of functions and data. We use classes because they allow us to write more readable and more maintainable code. We can achieve this by limiting access to the data and allow changes only through methods.

The simplest definition of a class uses the class keyword, the name of our class and a colon:

Class names start with a capital letter, while methods and functions use lower case letters. The pass keyword tells the compiler to do nothing. We need it here to define a class without methods and data.

To create an instance of our class, we write:

 

Special methods

Special methods in Python use double underscores before and after the method name (like __init__).

The __init__ special method is the initializer for a class that works like a constructor in other languages. This method gets executed whenever we create a new instance of our class. In the initializer we can enforce that every instance has certain values defined (like the name of a student):

We can create a new instance only when we specify the name of the student. If we omit it, we get this error:

The __str__ method is used when we print our instance (like ToString() in C#). The default implementation tells us the class name and the memory location – not the most user-friendly part of our instance:

We can overwrite the __str__ method with a more useful representation of our instance:

If we now print our instance, our own method is called:

 

Access modifiers

Python does not have access modifiers. Everything inside your class can be accessed from the outside. To separate things that should be accessed from those that no one outside our class should use, we can follow the convention to add a _ in front of the name (like _id or _name). This is only a weak indicator to not use this attribute or method outside of a class, but it works well enough. (see PEP 8)

 

Methods

The functions inside a class are called methods. This is just another name, but methods work the same way as functions.

There is only one speciality: The first parameter is always self and refers to the instance of the class:

Inside our methods we can access our private methods. We can call this method on our instance:

The self parameter allows us to call our method not only on our instance, but on our class as well. This longer and normally ignored way uses our instance as an argument:

 

Instance and class variables

Instance variables belong to the specific instance and differ from instance to instance. The self._name attribute is such an example that I used in the examples above.

Class variables belong to all instances of that class and should be the same from instance to instance. We create them directly under the class definition:

Those two constants are the same in all instances and we can call them directly on the class:

 

Next

There is much more than we can do with classes. Therefore, I wrote a second part where I explain how duck typing works.

2 thoughts on “Python Friday #9: Classes (Part 1)”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.