}

Python class the concepts you need to know

Created:

Introduction

I this tutorial we are going to explore some important python concepts, like python class, objects, modules, duck typing, etc. Being a python programmer is more than just writting code, it's a way of thinking. The objetive of this tutorial is to allow improve your python skills, however if you never used python before but have some previous programming experience this tutorial could be very helpful. This tutorial requires some basic knowledge of object oriented concepts.

The Python Class

A class is like a data type, like a dictionary, a list, a string, etc. We can think a class as something that groups data structures and methods that operate on those data structures. A class is like a blueprint of an objects, when a class is instantiated we call the result an intance of the class.

In python everything is an object, even classes. When classes are objects this is called First Class Types. To verify that a class is an object just try this on yout python console:

class Test:
    pass

type(Test)
>>> type

As you can see the type of the class is type.

Every class should have a single responsibility. It's very common to find classes that does many things, this is common since some programmers has the fear of adding new classes. Remember that classes and relationships it's a tool to help you to solve problems.

Do not fear of adding new classes when you start to think that a class has too many responsibilities.

Classes has methods that are defined with the keyword def, as an example:

class Duck:

    def quack(self):
        return 'Quack'

On this new example the most important concept is the variable self, which represent the instance on which the method was called.

As you can see in this class example we have two methods: * quack: a new method we defined.

In python class there are special names for methods.

  • The methods that has double underscore before and after are called magic methods (sometimes called dunder methods). The most well known magic methods is __init__ which is invoked when the object is created. See the appendix for more magic methods.
  • When a method starts with one underscore this mean that is a weak internal usage.
  • Underscore at the end of a method is usually used to avoid name conflicts with python reserved keywords, eg list and list_.

There are more uses for underscore, please check PEP0008 for more information.

Duck typing

This concept is not usually told to new python programmers and this i svery important way of thinking for python devs. Supose that you have an instance and you don't know the class of it, if someone calls the method quack of that instance and it raises an AttributeError this means that the instance of something is not a duck (or at least what we want right now as a duck). This is the concept of duck typing and it's very related to inheritance (actually is the best way of thinking about inheritance).

Appendix

Overview of Magic Methods

  • object.__add__(self, other)
  • object.__sub__(self, other)
  • object.__mul__(self, other) \/\/ object.__floordiv__(self, other) \/ object.__div__(self, other) % object.__mod__(self, other) ** object.__pow__(self, other[, modulo]) \<\< object.__lshift__(self, other) >> object.__rshift__(self, other) \& object.__and__(self, other) \^ object.__xor__(self, other) | object.__or__(self, other)