Lecture 10 Mutability, Data Abstraction
- Questions
- Constructor vs selector vs attributes, object oriented programming?
- How does python know what to do when minusing two instances of date without the method?
- For the dictionary example, where does values() come from?
- Data Abstraction
- Compound values combine other values together
- Lets us manipulate compound values as units
- Rational Numbers
- Exact representation of fractions
- A pair of integers
- Assume we can compose and decompose rational numbers
- Constructor
- rational(n,d)
- Selectors
- numer(x)
- denom(x)
- Representing Rational Numbers
- Construct a list
- numer and denom can select item from list
- Reducing to Lowest Terms
- Abstraction Barriers
- Use rational numbers to perform computation
- Create rationals or implement rational operations
- Implement selectors and constructor for rationals
- Implementation of lists
- ^ all the code above is separated by abstraction barriers
- Don’t need to know how code is implemented
- Violating Abstraction Barriers
- We just assume that our rationals are represented by lists
- Don’t do this, instead use the constructors and selectors
- Data Representations
- What are data?
- need to make sure constructor and selector
- Rationals Implemented as Functions
- Making a function that represents a rational number
- What are data?
- Mutability
- Objects
- behaves like what it represents
- represents information
- consist of data and behavior, bundled to create abstractions
- Object-oriented programming
- All objects have attributes
- A lot of data manipulation happens though object methods
- Objects
-
Example: Strings
- ASCII for Strings
-
Unicode Standard
- Abstraction that allows us to work with text
-
importing unicode
import unicodedata import lookup, name name('A') >>> 'LATIN CAPITAL LETTER A' lookup('SNOWMAN') >>> (Snowman Emoji that I can't type right now)
-
Lists
- .remove()
- search the list and remove that value without returning
- .pop()
- removing the value at index, but it does return what it took away
- .extend
- took the values inside a list and puts it in the list
- .append
- takes a value (not a list) and adds it to the end of the list
- original_suits and suits point to the same object
- .remove()
- Some objects can change
- the same object can change in value throughout the course of computation
- All names that refer t the same object are affected by a mutation
- Only objects of mutable types can change
- Mutation can happen within a function call
- A function can change the value of any object in its scope
- Doesn’t necessarily need to be passed in as an argument
- Tuples
- Create by using parentheses (high recommended)
- Immutable
- convert a list into a tuple to make it immutable
- Value of an expression can change because of
- name change
- object mutation
- An immutable sequence may still change if it contains a mutable value as an element
- We can change mutable values inside tuples
- Change the value of the list
- We can change mutable values inside tuples
- Adding tuples concatenate them
- Mutation
- Sameness and Changes
- A list is the “same” list even if we change its values
- Identity Operations
- Identity uses the “is” keyword
- Evaluates if they are the same object
- Equality uses “==”
- Evaluates if the values are the same
- Identical objects are always equal values
- Identity uses the “is” keyword
- Mutable Default Arguments are Dangerous
- A default argument value is part of a function value, not generated by a call
- default argument s is stored with the object
- s is the same list even between different calls
- Mutable Functions
- Modeling a bank account that has a balance of $100
- Stored the money in a mutable list in the global frame
- Name bound outside of withdraw def