17: Object-Oriented Programming
- Page ID
- 122426
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)As programs get to be millions of lines long, it becomes increasingly important to write code that is easy to understand. If you are working on a million line program, you can never keep the entire program in your mind at the same time. So we need ways to break the program into multiple smaller pieces so to solve a problem, fix a bug, or add a new feature we have less to look at. In a way, object oriented programming is a way to arrange your code so that you can zoom into 500 lines of the code, and understand it while ignoring the other 999,500 lines of code for the moment.
- 17.1: Managing Larger Programs
- This page discusses four essential programming patterns: sequential, conditional, repetitive code, and functions for code reuse. It emphasizes the importance of effective data structure design and elegant coding as program complexity increases. Clarity and organization are crucial for managing larger programs, and object-oriented programming is suggested as a method to simplify complexity by breaking code into smaller components.
- 17.2: Getting Started
- This page introduces essential concepts of object-oriented programming (OOP), focusing on object construction and functionality. It emphasizes utilizing existing objects in Python and its libraries, rather than creating new ones, to enhance future programming skills. The key takeaway is to understand how to effectively leverage available objects for ongoing learning and development.
- 17.3: Using Objects
- This page explores the use of objects in Python, highlighting a simple list operation and various list methods such as append and sort. It underscores the importance of object-oriented principles behind these operations. The dir() function is introduced as a tool for revealing an object's methods and attributes, with promises of deeper explanations in the chapter.
- 17.4: Starting with Programs
- This page explains program structure through an elevator conversion example, highlighting input processing and output generation. It focuses on object-oriented programming, where programs are divided into zones for specific tasks. Additionally, it discusses a URL link extraction application using BeautifulSoup, demonstrating the collaboration of different objects to process data and retrieve web information, emphasizing data movement orchestration within programs.
- 17.5: Subdividing a Problem - Encapsulation
- This page discusses the object-oriented approach, highlighting its ability to simplify complex systems through encapsulation. Users can interact with libraries like urllib and BeautifulSoup without needing to understand their internal workings, allowing them to focus on tasks. Developers also benefit by being able to work on components without requiring knowledge of every external detail. This enhances usability for both users and developers.
- 17.6: Our First Python Object
- This page explains that an object combines code and data structures defined by a class. A class serves as a template containing attributes and methods. The example of the PartyAnimal class shows how to create an object and utilize its method `party` to increment and display its attribute `x`. It clarifies the difference between calling a method on an instance and using the class directly.
- 17.7: Classes as Types
- This page explains how to examine variable types in Python using the `dir` and `type` functions. It introduces a custom class `PartyAnimal` with an integer attribute `x` and a method `party`. By creating an instance of `PartyAnimal`, the program illustrates how to print the object's type, its methods and attributes, and confirm the types of its properties, showcasing the introduction of new variable types through class creation in Python.
- 17.8: Many Instances
- This page explains creating multiple instances of a class in object-oriented programming using the `PartyAnimal` class in Python. It highlights the initialization of unique values for each instance through constructor parameters and illustrates this with two instances, `s` and `j`. The examples emphasize that each object has independent data attributes, with outputs demonstrating their separate behavior and interactions via the `party` method.
- 17.9: Object Lifecycle
- This page explains the creation and destruction of objects in Python, focusing on constructor (`__init__`) and destructor (`__del__`) methods. It illustrates how constructors initialize object values and destructors perform cleanup before object deletion. An example using a `PartyAnimal` class demonstrates these principles through object instantiation and destruction, highlighting the common use of constructors and the less frequent necessity of destructors.
- 17.10: Inheritance
- This page explains class extension in object-oriented programming through the creation of the CricketFan class from the PartyAnimal parent class. It details how CricketFan inherits attributes and methods, like the party method, and emphasizes the independence of instances from both classes while showcasing the enhanced features of the child class.
- 17.G: Object-Oriented Programming (Glossary)
- This page defines key concepts in object-oriented programming, such as attribute, class, child class, constructor, destructor, inheritance, method, object, and parent class, explaining their interactions. It also indicates where to find the list class's source code and recommends further exploration through Computer Science courses.
- 17.S: Object-Oriented Programming (Summary)
- This page provides an introduction to object-oriented programming, focusing on basic terminology, syntax, and methods for defining and using objects. It covers list object creation, method invocation (e.g., `append`, `sort`), element access, and the roles of constructors and destructors in managing object attributes and cleanup. The overview emphasizes that it does not delve into more complex object-oriented programming concepts.