python-course.eu

21. OOP Purely Functional

By Bernd Klein. Last modified: 24 Mar 2024.

This chapter is a hybrid belonging two OOP and functional programming, so like the following image which is a hybrid between a car and a Python snake. So we decided to include in this chapter only a short introduction and leave the bulk to be covered in "Functional Programming and OOP"

A car with a snake trunk

When we think of Object Oriented Programming we think about objects and their corresponding methods, we think about data encapsulation, which enables us to bundle data and methods together, shielding them from external interference and manipulation. The concept of getter-setter methods, which provide controlled access to the internal state of an object. By defining these accessors and mutators, we establish a standardized interface through which external code can interact with the object's data, ensuring integrity and consistency.

Functional programming mimics OOP by using local variables instead of private attributes and defining nested getter and setter functions to control access to inner state. Pure functions encapsulate behavior, while higher-order functions enable composition for modularity, achieving similar outcomes to OOP in a different manner.

Example

def Robot(name, city):

    def self():
        return None
    
    def get_name():
        return name

    def set_name(new_name):
        nonlocal name
        name = new_name

    def get_city():
        return city
        
    def set_city(new_city):
        nonlocal city
        city = new_city

    self.get_name = get_name
    self.set_name = set_name
    self.get_city = get_city
    self.set_city = set_city

    return self

# Create robot objects
marvin = Robot("Marvin", "New York")
r2d2 = Robot("R2D2", "Tatooine")

print(marvin.get_name(), marvin.get_city())
marvin.set_name('Marvin 2.0')
print(marvin.get_name(), marvin.get_city())

print(r2d2.get_name(), r2d2.get_city())
r2d2.set_city('Naboo')
print(r2d2.get_name(), r2d2.get_city())

OUTPUT:

Marvin New York
Marvin 2.0 New York
R2D2 Tatooine
R2D2 Naboo

We intentionally violated a fundamental Python convention by capitalizing a function name. Typically, function names should be in lowercase. However, we deliberately uppercased this function name to emphasize its resemblance to classes even more.

The example above corresponds to this class:

class Robot:
    def __init__(self, name, city):
        self._name = name
        self._city = city

    def get_name(self):
        return self._name

    def set_name(self, new_name):
        self._name = new_name

    def get_city(self):
        return self._city

    def set_city(self, new_city):
        self._city = new_city


# Create robot objects
marvin = Robot("Marvin", "New York")
r2d2 = Robot("R2D2", "Tatooine")

print(marvin.get_name(), marvin.get_city())
marvin.set_name('Marvin 2.0')
print(marvin.get_name(), marvin.get_city())

print(r2d2.get_name(), r2d2.get_city())
r2d2.set_city('Naboo')
print(r2d2.get_name(), r2d2.get_city())

OUTPUT:

Marvin New York
Marvin 2.0 New York
R2D2 Tatooine
R2D2 Naboo

Please continue now with "Functional Programming and OOP"

Wishing you an engaging and rewarding continuation of your journey with our Python course!

Car in Dusk

Live Python training

instructor-led training course

Enjoying this page? We offer live Python training courses covering the content of this site.

See: Live Python courses overview

Upcoming online Courses

Enrol here