Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PyConZA 2015: "What's the point of Object Orientation?" by Iwan Vosloo

Pycon ZA
October 02, 2015

PyConZA 2015: "What's the point of Object Orientation?" by Iwan Vosloo

Object Orientation (OO) is often introduced in terms of how it is implemented by a specific language. However, understanding the theory underlying OO is not quite the same as understanding how OO concepts are supported by a particular language. It is insightful to understand the simple OO fundamentals and how these map to the particular implementation provided by Python.

In this talk I will first explain the very basics of OO from a language-neutral point of view with the aim of showing what OO can offer you. I will touch upon the simple mathematical theory underlying OO and how it can be used as a mental discipline to improve your natural capacity to reason about programs. I hope to give you enough information to help you distinguish between better and worse designs and to detect whether you're using OO as it was intended. I will show how these fundamentals map to Python, and compare the difference of Python's implementation to that of some other languages (even functional languages).

This talk is for anyone: whether you're new at Object Orientation, or a practitioner wondering whether OO is worth the effort you've spent trying to use it.

Pycon ZA

October 02, 2015
Tweet

More Decks by Pycon ZA

Other Decks in Programming

Transcript

  1. PyConZA 2015 Introduction OOP: a part of OO Supporting mechanisms

    for OO Conceptual model of a problem domain Programming language map to Code
  2. PyConZA 2015 Summary OO concepts Object classifies Type 0..* subtype

    0..* Method implemented by 1..* Relation links 2..* Operation 1..* operates on
  3. PyConZA 2015 How can this help? Zoom in more def

    parse_file(price_file): for row in price_file: yield row.split(‘,’)
  4. PyConZA 2015 Implementing OO OOP and Python Object Oriented Programming

    Classical OOP Prototype Based supporting mechanisms Conceptual model map to Code
  5. PyConZA 2015 Python OOP basics Python cookies >>> class InvestmentInstrument:

    ... pass >>> fund = InvestmentInstrument() >>> fund <InvestmentInstrument object at 0x7f6815559fd0> >>> isinstance(fund, InvestmentInstrument) True >>> type(fund) <class ‘InvestmentInstrument’>
  6. PyConZA 2015 Python OOP basics Attributes >>> fund = InvestmentInstrument()

    >>> fund.is_active = True >>> fund.is_active True >>> other_fund = InvestmentInstrument() >>> other_fund.is_active = False >>> fund.is_active is other_fund.is_active False
  7. PyConZA 2015 Classical OOP Methods as (Python) methods Investment Instrument

    def activate(self): self.is_active = True indexed by class Operation
  8. PyConZA 2015 Python OOP basics A Python method class InvestmentInstrument:

    def activate(self): self.is_active = True >>> fund = InvestmentInstrument() >>> InvestmentInstrument.activate(fund) >>> fund.is_active True >>> fund.activate()
  9. PyConZA 2015 Python OOP basics Initialising instances class InvestmentInstrument: def

    __init__(self): self.is_active = False def activate(self): self.is_active = True >>> fund = InvestmentInstrument() >>> fund.is_active False >>> fund.activate()
  10. PyConZA 2015 Classical OOP Subtyping as inheritance class InvestmentInstrument: def

    __init__(self): self.is_active = False def activate(self): self.is_active = True class UnitTrustFund(InvestmentInstrument): def value_of(self, units): return units * self.unit_price
  11. PyConZA 2015 Classical OOP Subtyping as inheritance >>> fund =

    UnitTrustFund() >>> isinstance(fund, InvestmentInstrument) True >>> fund.activate() >>> fund.is_active True
  12. PyConZA 2015 Basic OO concepts The ghost of operations class

    CSV(FileFormat): def parse(self, a_file): for row in a_file: yield row.split(‘,’) class Excel(FileFormat): def parse(self, a_file): # totally different stuff
  13. PyConZA 2015 Basic OO concepts The ghost of operations for

    line in price_file.format.parse(price_file): # do stuff with line FileFormat PriceFile
  14. PyConZA 2015 Basic OO concepts The ghost of operations if

    price_file.format.is_csv: lines = parse_csv(price_file) elif price_file.format.is_excel: lines = parse_excel(price_file) else: raise Exception(‘not supposed to get here’) for line in lines: # do stuff with line
  15. PyConZA 2015 Design Worse design CommissionCalc Bundles QuoteBasis QuoteFees Commissions

    AbstractCommission Bundle * QuoteBenefit Benefit Summary Calc * Benefit Grouping * * QuoteBenefit Bundles Benefit Calc * Commission Bundle Altered CommissionBundle * Lookup CommissionScale LookupCommission ScaleFactory * Calculation Constants
  16. PyConZA 2015 Design Better design QuoteScenario Benefit Category * *

    * * CommisionBundle includedInAPI? negotiatedMax (R or %LOA) Commission Scale * Product VAT BenefitGrouping * Commission Band
  17. PyConZA 2015 Thanks www.reahl.org [email protected] Martin Fowler: Refactoring Martin Fowler:

    Analysis Patterns James Martin & James Odell: Object Oriented Methods: A Foundation