Lecture 16 Inheritance and String Representation
- Questions
- Repr will print quotes
- Calling on interpreter will not
- calling interpreter on obj equvialent to
- obj = print(repr(obj))
- str(obj) = obj.str()
- when using str, use the class of the object that is being used
- Review
- Attributes
- all objects have attributes
- name-value pairs
- Classes are objects too, so they have attributes
- Instance attribute: attribute of an instance
- Class attribute: attribute of the class of an instance
- Looking up attributes by name
.
- Class attributes
- “shared” across all instances of a class
- Attribute Assignment
- If the object is an instance, then assignment sets an instance attribute
- If the object is a class, then assignment sets a class attribute
- Attributes
- Inheritance
- relating classes together
- Specialized class may have the same attributes as the general class, along with some special case behavior
- Example: Checking Account
- Two options for withdrawing
- super()
- creates a temporary object of the parent account
- Looking up attribute names on classes
- Base class attributes aren’t copied into subclasses
- Looking up a name in a class
- If it names an attribute in the class, return the attribute value
- Otherwise, look up the name in the base class, if there is one
- Modifying the class attribute in the base class, affects the child class
- Class Relationships
- You can make a child class of a child class
- Class Hierarchy
- Account
- Checking Account, Savings Account
- SuperSaverAccount
- Checking Account, Savings Account
- Account
- Object-Oriented Design
- Don’t need to repeat ourselves if we just use existing implementations
- Inheritance and Composition
- Inheritance is best for representing is-a relationships
- A checking account is a specific type of account
- Checking Account inherits from Account
- Inheritance is best for representing is-a relationships
- Multiple Inheritance
- Can inherit multiple classes
- Methods come from the different parents of the As Seen on TV Account
- If methods overlap
- Look left to right to see who has precedence
- Left has priority
- Representation
- String Representations
- An object value should behave like the kind of data it is meant to represent
- In python all objects produce two string representations
- The str is legible to humans
- the repr is legible to the Python Interpreter
- repr String for an Object
- repr(object) → string
- It returns the string to return the object
- The result of calling repr on a value is what Python prints in an interactive session
- Some objects do not have a simple Python-readable string
-
repr(min)
- ‘
’
-
- str String representation for an Object
- Human interpretable strings are useful as well
- str gives a more human readable version
- Result of calling str on the value of an expression is what Python prints using the “print” function
- String Representations
>>> repr(half)
'Fraction(1,2)'
>>>str(half)
'1/2'
-
F-Strings
- String Interpolation in Python
- evaluating a string literal that contains expresssions
- Putting brackets around our string to display the value
- Result of evaluating an f-string literatal contains the str value of each subexpression
>>> f"add 1 + 2 = {1 + 2}" 'add 1 + 2 = 3'
- String Interpolation in Python
Making our own str and repr
def __str__(self):
return f"Holder: {self.holder}\nBalance: {self.balance}"
def __repr__(self):
return f"Account('{self.holder}')"
#print result has the actual result
#str(a) is the actual string it self, so the \n is not honored