Python OOP

11/02/2025

Object oriented programming accounts to one of an myriad of programming paradigms (imperative, functional, declarative, logic…). Hypothetically all can be present in a single program. 

To reuse code for creating objects, we define a blueprint by a class.

From the class, objects an object (instance) can be created. In our case a Toyota from a Car.

Each object has some state and behavior.

The State

Represents the data or attributes of an object, stored in instance variables, defining the characteristics of the object at a particular point in time.

The Behavior

Encapsules the actions or operations that an object can perform, defined by methods, The behavior controls how the object interacts with data or other objects.

Methods modify the internal state of the instance. Methods are very similar to a functions, but closely related to instances. Instances can call methods and vice versa. Comparably, functions would be defined outside a class.

A method is defined in a class, starting with the self argument. The first method of a class needs to be the self-calling method __init__ with the parameter self. There are in Python special methods recognized the double leading and tailing underscores.

Module – a file (.py) with definitions and statements (ref).

they can be then accessed 

or renamed.

The design is modular, so changes can be made just to modules, without the need to large-scale adjustments.

Getters and setters

in order to help with the encapsulation principle, Python introduces Setters and Getters.

Getters → Get the value of an attribute.

Setters → Set the value of an attribute.

The right way to use a getter would be

But this would not work:

By using a getter, the attribute is protected

There is also a Pythonic way how to set getters (link). Getters and setters are used in JavaScript, as well Vuex. Vuex is an extension of Vue.js. Alternatives are React, Angular, Bootstrap. All have advantages and disadvantages.

A setters would be user to check the name for errors, before being set my_go.set_name("Norita")

Here an overview of basic principles in OOP of Python.

Let us see the usage of python in running a server

 Similarly to JavaScript, Python has as well lambda functions.

Share
Rafael Schwarzenegger © All rights reserved 2021
Powered by Webnode
Create your website for free! This website was made with Webnode. Create your own for free today! Get started