Opportunity Through Data Textbook
  • Opportunity Through Data Textbook
  • Introduction
    • What is Data Science?
    • Introduction to Data Science: Exploratory Musical Analysis
  • Module 1
    • Introduction to Programming
      • The Command Line
      • Installing Programs
      • Python and the Command Line
      • Jupyter Notebook
    • Introduction to Python
      • Building Blocks of Python - Data Types and Variables
      • Functions
      • Formatting and Syntax
    • Math Review
      • Variables and Functions
      • Intro to Graphs
  • Module 2
    • Data Structures
      • Lists
      • Dictionaries
      • Tables
    • Programming Logic
      • Loops
      • Logical Operators
      • Conditionality
  • Module 3
    • Introduction to Probability
      • Probability and Sampling
    • Introduction to Statistics
      • Mean & Variance
      • Causality & Randomness
  • Module 4
    • Packages
    • Intro to NumPy
      • NumPy (continued)
  • Module 5
    • Introduction to Pandas
      • Introduction to Dataframes
      • Groupby and Join
    • Working with Data
    • Data Visualization
      • Matplotlib
      • Introduction to Data Visualization
  • Appendix
    • Table Utilities
    • Area of More Complicated Shapes
    • Introduction to Counting
    • Slope and Distance
    • Short Circuiting
    • Linear Regression
    • Glossary
  • Extension: Classification
    • Classification
    • Test Sets and Training Sets
    • Nearest Neighbors
  • Extension: Introduction to SQL
    • Introduction to SQL
    • Table Operations
      • Tables and Queries
      • Joins
  • Extension: Central Limit Theorem
    • Overview
    • Probability Distributions
      • Bernoulli Distribution
      • Uniform Distribution (Discrete)
      • Random Variables, Expectation, Variance
      • Discrete and Continuous Distributions
      • Uniform Distribution (Continuous)
      • Normal Distribution
    • Central Limit Theorem in Action
    • Confidence Intervals
  • Extension: Object-Oriented Programming
    • Object-Oriented Programming
      • Classes
      • Instantiation
      • Dot Notation
      • Mutability
  • Extension: Introduction to Excel
    • Introduction to Excel
      • Terminology and Interface
      • Getting Started with Analysis and Charts
      • Basics of Manipulating Data
    • Additional Features in Excel
      • Macros
      • The Data Tab
      • Pivot Tables
Powered by GitBook
On this page

Was this helpful?

  1. Extension: Object-Oriented Programming
  2. Object-Oriented Programming

Mutability

Mutable objects have the ability to change their attributes after they are created.

One advantage of OOP is that our objects are mutable! This means that we can change the variables or attributes of any of our objects after instantiation. For example, Fido won’t be 5 years old forever, so we can manually change our dog’s age.

>>> fido.age = 6
>>> fido.age
6

As you can see above, when we call fido.age, the program will now return 6, not 5. We can also define a new method that can increment his age for us, so we don’t have to manually update it with 7, 8, 9...etc. forever. We use the self keyword once again to signify which specific object’s instance attribute we are changing.

def have_birthday(self): 
	self.age += 1	
	print(self.name + " is now " + str(self.age) + " years old. Happy birthday!!!")
	# str() converts integers into strings
	
>>> fido.have_birthday()
Fido is now 7 years old. Happy birthday!!!
>>> fido.age
7

Conclusion

In short, a python class defines a type of object. They define what attributes the object can have and what methods can be used to change the state of an object, and this method of programming is called object-oriented programming. OOP helps programmers cope with the complexity of large systems.

PreviousDot NotationNextIntroduction to Excel

Last updated 4 years ago

Was this helpful?