0% found this document useful (0 votes)
76 views33 pages

Object Orientation vs. Functional Programming: Writing Modular Python Programs

The document discusses object-oriented and functional programming approaches in Python. It covers various object-oriented programming concepts like inheritance, composition, abstract base classes, mixins and properties. It also covers functional programming concepts like callbacks, higher-order functions, decorators, partial functions and use of built-in modules like itertools. Finally, it discusses how Python supports both paradigms and how they can be combined to get the best of both worlds.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views33 pages

Object Orientation vs. Functional Programming: Writing Modular Python Programs

The document discusses object-oriented and functional programming approaches in Python. It covers various object-oriented programming concepts like inheritance, composition, abstract base classes, mixins and properties. It also covers functional programming concepts like callbacks, higher-order functions, decorators, partial functions and use of built-in modules like itertools. Finally, it discusses how Python supports both paradigms and how they can be combined to get the best of both worlds.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Object Orientation vs.

Functional Programming
Writing Modular Python Programs

Twitter: @insmallportions www.insmallportions.com

About Me

Modularity

Roadmap
Thesis
Object Orientation is a proven way of creating models in software that represent the problem domain in a useful manner. There are many patterns that show how to achieve the modularity goal in different contexts.

Antithesis
Functional Programming is a long standing approach to defining processes in terms of others at different levels of abstraction. Higher order functions make expressing reusable algorithms natural and customising them easy.

Synthesis
Python has good support for both styles of programming and for good reason. Depending on the situation one or the other maybe more appropriate. Moreover in Python these tools do not only exist but they complement each other.

Object Orientation
Class Oriented The Three Pillars of OO in Python: 1. Delegation 2. Polymorphism 3. Instantiation

Template Method
class Game(object): PLAYERS = 2 def initialize_game(self): raise NotImplementedError() def make_play(self, player): raise NotImplementedError() def end_of_game(self): raise NotImplementedError() def print_winner(self): print self.current def play_game(self, players=PLAYERS): self.initialize_game() self.current = 0 while not self.end_of_game(): self.make_play(self.current) self.current = (self.current + 1)\ % players self.print_winner() class Monopoly(Game): PLAYERS = 4 def initialize_game(self): pass # Monopoly code here def make_play(self, player): pass # Monopoly code here def end_of_game(self): pass # Monopoly code here def print_winner(self): pass # Monopoly code here class Chess(Game): def initialize_game(self): pass # Chess code here def make_play(self, player): pass # Chess code here def end_of_game(self): pass # Chess code here

Abstract Base Classes


>>> class MyDict(dict): ... def __getitem__(self, key): ... return 101 ... >>> d = MyDict() >>> d['x'] 101 >>> d.get('x', 202) 202 >>> >>> from collections import Mapping >>> class >>> from collections import Mapping >>> class MyMapping(Mapping): ... def __getitem__(self, key): ... return 101 ... >>> m = MyMapping() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Can't instantiate abstract class MyMapping with abstract methods __iter__, __len__

Mixins
class XMPPClient(object): def connect(self): pass # XMPP code def disconnect(self): pass # XMPP code def send(self, player): pass # XMPP code def terminate(self, player): raise NotImplementedError() def mainain_presence(self): self.connect() while not self.terminate(): yield self.disconnect()

class OnlineChess (Game, XMPPClient): def initialize_game(self): pass # Chess code here

...
def end_of_game(self): pass # Chess code here def terminate(self, player): return self.end_of_game()

Mixins (Multiple Inheritance)


class A(object): pass class B(A): def method1(self): pass class C(A): def method1(self): pass class D(B, C): pass

Wrapping/Composition
Prefer Composition over Inheritance Use a class's functionality but not its API Expose only limited part of an object Typical uses: Adapt Proxy Decorate

Wrapping/Composition
class Eprom(object): def read(self): pass # Eprom code def write(self, data): pass # Eprom code def complete(self): pass # Eprom code class FileLikeEprom(object): def __init__(self, eprom): self._eprom = eprom def read(self): return self._eprom.read() def write(self, data): self._eprom.write(data) def close(self): self._eprom.complete() class SafeEprom(object): def __init__(self, eprom): self._eprom = eprom def read(self): return self._eprom. read() def write(self, data): if safe(data): self._eprom.write (data) def close(self): self._eprom.complete()

Wrapping/Composition (Tricks)
Don't Repeat Yourself Avoid boilerplate Use __getattr__ to return computed attributes
class FileLikeEprom(object): def __init__(self, eprom): self._eprom = eprom def __getattr__(self, n): if n == 'close': return self.close else: return getattr(self._eprom, n) def close(self): self._eprom.complete()

Mixins Again
class SafeAndFileLike(FileLikeEprom, SafeEprom): def __init__(self, *args, **kwargs): return super(SafeAndFileLike, self).__init__(*args, **kwargs)

Roadmap
Thesis
Object Orientation is a proven way of creating models in software that represent the problem domain in a useful manner. There are many patterns that show how to achieve the modularity goal in different contexts.

Antithesis
Functional Programming is a long standing approach to defining processes in terms of others at different levels of abstraction. Higher order functions make expressing reusable algorithms natural and customising them easy.

Synthesis
Python good support for both styles of programming and for good reason. Depending on the situation one or the other maybe more appropriate. Moreover in Python these tools do not only exist but they complement each other.

Functional Programming
Functions take input and produce output, without any side effects. Pure functional languages are strict about side effect freeness. Python is not a pure functional language. Functions may be internally imperative, but appear purely functional in their behaviour.

Callbacks
The Hollywood principle Role reversal, library code calls your code Library code accepts a callable and invokes it when appropriate The main uses: Customisation Event Handling

sorted() sans Callbacks


class Person(object): def __init__(self, f, s): self.f = f self.s = s def __str__(self): return '%s %s' % (self.f, self. s) def __eq__(self, other): return self.s == other.s def __lt__(self, other): return self.s < other.s >>> people = [Person ('John', 'Smith'), ... Person('Mary', 'Doe'), ... Person('Lucy', 'Pearl'),] >>> for p in sorted(people): ... print p ... Mary Doe Lucy Pearl John Smith >>>

sorted() with Callbacks


class Person(object): def __init__(self, f, s): self.f = f self.s = s def __str__(self): return '%s %s' % (self.f, self. s) first_name = lambda p: p.f surname = lambda p: p.s >>> for p in sorted(people, key=first_name): ... print p ... John Smith Lucy Pearl Mary Doe >>> for p in sorted(people, key=surname_name): ... print p ... Mary Doe Lucy Pearl John Smith >>>

operator module
attrgetter itemgetter add mul pow ...
from operator import attrgetter class Person(object): def __init__(self, f, s): self.f = f self.s = s def __str__(self): return '%s %s' % (self.f, self. s) first_name = attrgetter('f') surname = attrgetter('s')

Operations on aggregates
map filter reduce sum
>>> def square(x): ... return x ** 2 ... >>> s = [1, 2, 3, 4, 5] >>> sum(map(square, s)) 55 >>> def odd(x): ... return x % 2 ... >>> s = [1, 2, 3, 4, 5] >>> sum(map(square, filter(odd, s))) 35

itertools module
cycle() repeat() chain() tee() product() ...

Decorators
def cache(fn, c=None): if c is None: c = {} def cached(*args): if args in c: return c[args] result = fn(*args) c[args] = result return result return cached def adder(x, y): return x + y adder = cache(adder) def cache(fn, c=None): if c is None: c = {} def cached(*args): if args in c: return c[args] result = fn(*args) c[args] = result return result return cached @cache def adder(x, y): return x + y

Do not write code like this, use: functools.lru_cache

Partial function evaluation


>>> from functools import partial >>> >>> def power(base, exp=1): ... return base ** exp ... >>> square = partial(power, exp=2) >>> cube = partial(power, exp=3) >>> >>> l = [1, 2, 3, 4, 5] >>> sum(map(square, l)) 55 >>> print sum(map(cube, l)) 225

Roadmap
Thesis
Object Orientation is a proven way of creating models in software that represent the problem domain in a useful manner. There are many patterns that show how to achieve the modularity goal in different contexts.

Antithesis
Functional Programming is a long standing approach to defining processes in terms of others at different levels of abstraction. Higher order functions make expressing reusable algorithms natural and customising them easy.

Synthesis
Python good support for both styles of programming and for good reason. Depending on the situation one or the other maybe more appropriate. Moreover in Python these tools do not only exist but they complement each other.

Best of Both Worlds

Unbound methods
>>> food = ['Spam', 'ham', 'Cheese', 'eggs'] >>> sorted(food) ['Cheese', 'Spam', 'eggs', 'ham'] >>> sorted(food, key=str.lower) ['Cheese', 'eggs', 'ham', 'Spam'] >>>

>>> sorted(food, key='ham'. lower) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: lower() takes no

Functions are descriptors Override binding behaviour Override differently for A.x and a.x Unbound methods know their class but not their instance Ideal for use in a functional style

Computed fields (property)


class Person(object): def __init__(self, f, s): self.f = f self.s = s @property def fullname(self): return '%s %s' % (self.f, self.s) >>> p = Person('John', 'Smith') >>> p.fullname 'John Smith' class Person(object): def __init__(self, f, s): self.f = f self._s = s @property def s(self): return self._s.upper() @s.setter def s(self, value): self._s = value >>> p = Person('Jane', 'Doe') >>> p.s 'DOE'

property([fget[, fset[, fdel[, doc]]]])

property and inheritance


class Person(object): class Person(object): def __init__(self, t, f, s): def __init__(self, t, f, s): ... ... def full(self): def full(self): return '%s %s' % (self.f, self. return '%s %s' % (self.f,self. s) s) def _full(self): fullname = property(full) return self.full() class Customer(Person): fullname = property(_full) def full(self): class Customer(Person): return '%s. %s %s' % (self.t, self.f, self.s) def full(self): return '%s. %s %s' % >>> c = Customer('Mr', 'John', t, self.f, self.s) 'Smith') >>> c.fullname >>> c.fullname 'Mr John Smith' 'John Smith'

Dependency Inversion
class Employee(object): def __init__(self, f, s): self.f = f self.s = s def register(self): pass # Register me def register(emps): for f, s in emps: emp = Employee(f, s) emp.register() >>> emps = [('John', 'Smith'), ('Mary', 'Doe')] >>>register(emps) def employee_fact(f, s): return Employee(f, s) def register(emps, fact): for f, s in emps: emp = fact(f, s) emp.register() >>> emps = [('John', 'Smith'), ('Mary', 'Doe')] >>>register(emps, employee_fact)

Python classes are factories


Python classes are callables class Employee(object): def __init__(self, f, s): Indistinguishable from other self.f = f callables to the caller self.s = s Allows us to postpone the def register(self): creation of a factory until it is pass # Register me actually needed def register(emps, fact): for f, s in emps: emp = fact(f, s) emp.register() >>> emps = [('John', 'Smith'), ('Mary', 'Doe')] >>>register(emps, Employee)

Many types of callables


Functions Unbound methods Bound methods Classes
Any object that has a __call__ method is a callable Testable using the callable built-in function >>> callable(str) True >>> callable('Spam') False >>> class Callable(object): def __init__(self, m): self.message = m def __call__(self): print self.message class NotCallable(object): def call(self): print "You Rang?"
>>> c = Callable('You Rang') >>> c() You Rang >>> n = NotCallable() >>> n() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'NotCallable' object is not callable

Roadmap
Thesis
Object Orientation is a proven way of creating models in software that represent the problem domain in a useful manner. There are many patterns that show how to achieve the modularity goal in different contexts.

Antithesis
Functional Programming is a long standing approach to defining processes in terms of others at different levels of abstraction. Higher order functions make expressing reusable algorithms natural and customising them easy.

Synthesis
Python good support for both styles of programming and for good reason. Depending on the situation one or the other maybe more appropriate. Moreover in Python these tools do not only exist but they complement each other nicely.

We hire superheroes!
www.demonware.net/jobs/ Development & Operations Positions Come talk to us

You might also like