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 What’s the point of Object Orientation? Iwan Vosloo

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

    for OO Conceptual model of a problem domain Programming language map to Code
  3. PyConZA 2015 Introduction Cause and effect

  4. PyConZA 2015 Introduction Scaling up Courtesy: inkwellideas.com 100 lines of

    code?
  5. PyConZA 2015 Introduction How about... 180 000 lines of code?

  6. PyConZA 2015 Introduction Understanding opportunity useless information danger

  7. PyConZA 2015 Concepts A conceptual model Chair

  8. PyConZA 2015 Concepts Concepts as sets Chair Desk

  9. PyConZA 2015 Concepts Refinement via subsets Chair Office Chair

  10. PyConZA 2015 Relations Connecting objects Chair Desk

  11. PyConZA 2015 Relations Relation as a concept Assignment

  12. PyConZA 2015 Relations Multiplicity Chair Desk

  13. PyConZA 2015 Notation It gets complicated... Chair Desk Assignment Office

    Chair
  14. PyConZA 2015 Notation UML Desk Chair OfficeChair assignment 1 *

    Warning !!
  15. PyConZA 2015 More on concepts Intangible concepts Portfolio Investment Instrument

    UnitTrust
  16. PyConZA 2015 More on concepts Overlapping concepts Person Investor

  17. PyConZA 2015 More on concepts Changing classification Person Employee Investor

  18. PyConZA 2015 More on concepts Concept labels Concept Synonym Type

    Concept Label Chair
  19. PyConZA 2015 Affecting objects Operations Price PriceFile UnitTrust Fund *

    load_prices( )
  20. PyConZA 2015 Affecting objects Methods Operation Object 1..* operates on

    Method 1..* 1 is implemented by
  21. PyConZA 2015 Summary OO concepts Object classifies Type 0..* subtype

    0..* Method implemented by 1..* Relation links 2..* Operation 1..* operates on
  22. PyConZA 2015 How can this help? Programs are programs Courtesy:

    inkwellideas.com Concept Concept
  23. PyConZA 2015 How can this help? Understanding-structured Courtesy: inkwellideas.com parse_file:

    Operation parse_exel: Method parse_csv: Method
  24. PyConZA 2015 How can this help? Focus on one thing

    FileFormat PriceFile
  25. PyConZA 2015 How can this help? Zoom in FileFormat Excel

    CSV
  26. PyConZA 2015 How can this help? Zoom in more def

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

    Classical OOP Prototype Based supporting mechanisms Conceptual model map to Code
  28. PyConZA 2015 Classical OOP Types as classes an instance an

    instance an instance
  29. 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’>
  30. PyConZA 2015 Classical OOP Relationships as attributes Investment Instrument bool

    True False is_active is_active is_active
  31. 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
  32. PyConZA 2015 Classical OOP Methods as (Python) methods Investment Instrument

    def activate(self): self.is_active = True indexed by class Operation
  33. 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()
  34. 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()
  35. 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
  36. PyConZA 2015 Classical OOP Subtyping as inheritance >>> fund =

    UnitTrustFund() >>> isinstance(fund, InvestmentInstrument) True >>> fund.activate() >>> fund.is_active True
  37. PyConZA 2015 Classical OOP The ghost of operations FileFormat Excel

    CSV parse( )
  38. 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
  39. PyConZA 2015 Basic OO concepts The ghost of operations for

    line in price_file.format.parse(price_file): # do stuff with line FileFormat PriceFile
  40. 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
  41. PyConZA 2015 Design What’s design? Person Employee Investor Person Role

    Employee Investor Company
  42. 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
  43. PyConZA 2015 Design Better design QuoteScenario Benefit Category * *

    * * CommisionBundle includedInAPI? negotiatedMax (R or %LOA) Commission Scale * Product VAT BenefitGrouping * Commission Band
  44. PyConZA 2015 Design Inheritance vs subtyping Unordered List Menu Menu

    Unordered List
  45. PyConZA 2015 Thanks www.reahl.org iwan@reahl.org Martin Fowler: Refactoring Martin Fowler:

    Analysis Patterns James Martin & James Odell: Object Oriented Methods: A Foundation
  46. PyConZA 2015 Design Inheritance vs subtyping ObjectTable ObjectTable WithFilters ObjectTable

    Filter 0..* Report ObjectTable Filter * ObjectTable ByDate