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 introduces four core programming patterns: sequential, conditional, repetitive code, and functions. It emphasizes the significance of designing clear data structures and maintaining organized code as program size increases. Breaking down code into manageable parts is essential for clarity. The text highlights object-oriented programming as a strategy to manage large codebases, enabling focused understanding and modification of specific sections while temporarily ignoring the rest.
- 17.2: Getting Started
- This page introduces object-oriented programming concepts, stressing the importance of understanding key terms and principles. It aims to lay a foundational understanding through simple examples, focusing on object construction, functionality, and utilizing Python and its libraries for future programs.
- 17.3: Using Objects
- This page discusses built-in objects in Python, focusing on list operations such as `append()`, `sort()`, and `__getitem__()`. It highlights the use of method calls alongside bracket notation for accessing list elements. The `dir()` function is also introduced, providing a way to list an object's methods and attributes. The chapter aims to enhance understanding of object-oriented programming through these examples.
- 17.4: Starting with Programs
- This page outlines the structure of programs, using examples like an elevator conversion tool and URL extraction application. It describes how programs function by taking input, processing data, and generating output, often interacting with external elements. The concept of object-oriented programming is highlighted, presenting programs as networks of objects with specific roles.
- 17.5: Subdividing a Problem - Encapsulation
- This page discusses the object-oriented approach, highlighting its benefit of encapsulation, which hides complexity and lets users leverage libraries like urllib and BeautifulSoup. This enables developers to focus on solving specific problems without needing to comprehend the internal workings of these tools, thereby simplifying the development process and improving usability.
- 17.6: Our First Python Object
- This page explains that an object is a mix of code and data, created from a class defined by the class keyword. A class acts as a blueprint for objects, which hold attributes and methods. The example provided is the PartyAnimal class, featuring an attribute x and a method party that increments x. Objects created from classes let users access their data and methods, with distinctions made between method and global function calls, affecting output for x.
- 17.7: Classes as Types
- This page discusses Python variable types and the use of the built-in `dir` function to explore a variable's capabilities. It includes an example with a `PartyAnimal` class, demonstrating how to create a new type and utilizing `type` and `dir` to show a class instance's type, attributes, and methods, thus illustrating interaction with custom classes in Python.
- 17.8: Many Instances
- This page explains creating multiple instances of a class in object-oriented programming using constructors. It illustrates the `PartyAnimal` class, where each instance, like `s` and `j`, has unique attributes (`name` and `x`), allowing for independent behavior. The example demonstrates how parameters are passed to the constructor for unique initial values, leading to separate outputs when methods are called for each object.
- 17.9: Object Lifecycle
- This page discusses managing object lifecycles in Python using constructors and destructors. It explains the role of the constructor method (__init__) in initializing object values and the destructor method (__del__) in handling cleanup before the object is discarded. An example with a PartyAnimal class illustrates the process, highlighting that constructors are frequently used while destructors are less common.
- 17.10: Inheritance
- This page discusses object-oriented programming, specifically inheritance, where a new class (child) is created by extending an existing class (parent). The example involves the CricketFan class extending the PartyAnimal class, inheriting its attributes and methods. Instances of both classes maintain their own states, showcasing inheritance, while the CricketFan class adds its unique methods and attributes. The output highlights the combined functionalities of these instances.
- 17.G: Object-Oriented Programming (Glossary)
- This page outlines key principles of object-oriented programming, defining terms like attributes, classes, inheritance, and methods. It describes classes as templates for objects, explains how child classes inherit from parent classes, and discusses the role of constructors and destructors in object lifecycle management. It also includes references to C source code for a list class and recommends further exploration of computer science courses.
- 17.S: Object-Oriented Programming (Summary)
- This page introduces object-oriented programming, focusing on terminology and syntax in Python. It explains the creation of list objects using the constructor method (__init__), encapsulation, and provides example code for appending, sorting, and accessing elements. It also mentions the destructor method (__del__) for cleanup. The content concludes by stating that more in-depth discussions on object-oriented approaches are not covered.