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"
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!
Live Python training
Upcoming online Courses
24 Feb 2025 to 28 Feb 2025
31 Mar 2025 to 04 Apr 2025
07 Apr 2025 to 11 Apr 2025
19 May 2025 to 23 May 2025
02 Jun 2025 to 06 Jun 2025
30 Jun 2025 to 04 Jul 2025
11 Aug 2025 to 15 Aug 2025
10 Mar 2025 to 14 Mar 2025
07 Apr 2025 to 11 Apr 2025
23 Jun 2025 to 27 Jun 2025
28 Jul 2025 to 01 Aug 2025
12 Mar 2025 to 14 Mar 2025
09 Apr 2025 to 11 Apr 2025
04 Jun 2025 to 06 Jun 2025
30 Jul 2025 to 01 Aug 2025
Machine Learning from Data Preparation to Deep Learning
10 Mar 2025 to 14 Mar 2025
07 Apr 2025 to 11 Apr 2025
02 Jun 2025 to 06 Jun 2025
28 Jul 2025 to 01 Aug 2025