aka:Python:memo
- Python Object System Memo
- Motivation
- I'm learning Python Object System with Introduction to Computer Science with Python from MIT Press.
- The chapter 8 is dedicated to the object system, which is surprisingly boring...
- I try to build understanding on the object system by writing my reading memo of the chapter here.
- 8 Classes and Object-Oriented Programming
- (Introduction)
- The purpose of this chapter is to learn how to use classes to organize programs around modules and data abstractions.
- We'll focus on object-oriented programming while classes can be used in many different ways.
- 8.1 Abstract Data Types and Classes
- Abstract Data Type
- An abstract data type is:
- "a set of objects and the operations on those objects. These are bound together so that one can pass an object from one part of a program to another, and in doing so provide access not only to the data attributes of the object but also to operations that make it easy to manipulate that data."
- I don't understand this definition of "an abstract data type", because id doesn't sound "abstract" at all...
- The definition on Wikipedia looks much easier to understand:
- "... an abstract data type (ADT) is a mathematical model for a certain class of data structures that have similar behavior; or for certain data types of one or more programming languages that have similar semantics. An abstract data type is defined indirectly, only by the operations that may be performed on it and by mathematical constraints on the effects (and possibly cost) of those operations."
- This better definition still has some unclear points. For example, what's the difference between data structures and data types? Here are definitions from Wikipedia.
- Data structure: "... a data structure is a particular way of organizing data in a computer so that it can be used efficiently. Different kinds of data structures are suited to different kinds of applications, and some are highly specialized to specific tasks. For example, B-trees are particularly well-suited for implementation of databases, while compiler implementations usually use hash tables to look up identifiers."
- Data type: "... a data type or simply type is a classification identifying one of various types of data, such as real, integer or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of that type can be stored."
- Wikipedia also has a section called 'Definition of a "type", which are quoted from (Parnas, Shore & Weiss 1976). But this looks like not helpful for understanding types in this memo because it's a list of implicit definition of types on literatures which are problematic.
- So there is a practical difference about the definition of ADT between this book and other world.
------This book calls the docstring specifications as interface while it is ADT itself in other world.
- "Programming is about managing complexity in a way that facilitates change. There are two powerful mechanisms available for accomplishing this: decomposition and abstraction."
- Abstraction here means ADT or Domain Specific Types.
- Introducing DSL here is interesting. If a language has a good set of general features, creating a DSL is equivalent to creating DSTs.
- Then, what is decomposition here?
- In Python, you use classes to implement data abstractions.
- Representation Invariant
- "An object invariant, or representation invariant, is a computer programming construct consisting of a set of invariant properties that remain uncompromised regardless of the state of the object."
- This is a notion from design by contract.
- Representation or class invariant is natively supported by languages such as Ada, Eiffel, and D.
- 8.1.1 Designing Programs Using Abstract Data Types
- 8.1.2 Using Classes to Keep Track of Students and Faculty
- 8.2 Inheritance
- 8.2.1 Multiple Levels of Inheritance
- 8.2.2 The Substitution Principle
- 8.3 Encapsulation and Information Hiding