Object-oriented programming in Python

Data Engineering @ Statnett


Simeon Simeonov

Goals


  • Present the Python programming language in a different way than https://docs.python.org

  • Avoid information overload

  • Use examples and interaction rather than documents and slides

Preliminary plan


  • Basics: About the language, the Python eco-system, types, modules, functions, scopes, decorators, string formatting

  • Object-oriented programming in Python: How Python "really works"

  • Control flow: if / for / while / try, iterators, "tactical programming" tips

  • A brief tour through Python's standard library

  • Code design and best practices: How to design your code

What is object-oriented programming?


Object-oriented programming does not mean using an object-oriented programming language.

It is rather a programming paradigm based on the concept of object, as well as on some general principles and best practices aiming at:

  • improving readability

  • improving re-usability

  • improving modularity

  • providing foundation for a more intuitive design

General principles


The following four concepts / principles are presented in most object-oriented programming books:

  • separate and hide "private" details from the outside world and / or child (inheriting) functionality (encapsulation)

  • separate the interface from its implementation (abstraction)

  • inherit and extend / adapt existing functionality, through "is-a" relationship hierarchy (inheritance)

  • execute different code / functionality based on the object's place in the hierarchy (polymorphism)

Building blocks and definitions


Note: "Lacking universally accepted terminology to talk about classes, I will make occasional use of Smalltalk and C++ terms." - The Python tutorial (https://docs.python.org)

When talking about object-oriented programming, the following building blocks are involved:

  • class - a blueprint / template for creating objects

  • object - an instance of a class that may contain its own attributes as well as references to its class' attributes

  • attribute - variable, property, function defined in the class and present in its instances

  • class variable - attribute of which a single copy exists, regardless of how many instances of the class exist

  • object / instance variable - attribute for which each instantiated object of the class has a separate copy, or instance

  • method - member function - function that is an attribute

The object-oriented world of Python


Some additional concepts and definitions are introduced in Python, as well as in some other programming languages:

  • metaclass - a class whose instances are classes

  • property - attribute that provides a flexible mechanism to read, write, or compute the value of a "private" attribute

  • type - old C object-oriented term, now (in Python 3) considered to be the same as class

As is true for modules, classes partake of the dynamic nature of Python: they are created at runtime, and can be modified further after creation.

The object-oriented world of Python (cont...)


The <

  • metaclass - a class whose instances are classes

  • property - attribute that provides a flexible mechanism to read, write, or compute the value of a "private" attribute

  • type - old C object-oriented term, now (in Python 3) considered to be the same as class