Lecture 15 Objects
- Questions
- Object-Oriented Programming
- A method for organizing programs
- Data Abstraction
- Bundling together info and related behavior
- Computation using distributed state
- Each object has its own local state
- Each object also knows how to manage its own local state, based on method calls
- Method calls are messages passed between objects
- Different types may relate to each other
- A method for organizing programs
- Classes
- A class describes the general behavior of its instances
- Class vs Objects
- A class combines and abstracts data and functions
- An object is an instantiation of a class
-
Class Statements
- The Class Statement
- Note: By convention, class names start with a capital letter
- Class statement creates a new class and bines that class to
in the first frame of the current environment - Assignment and def statements in
create attributes of the class
class <name>: <suite> color = 'red' windows = 2
- When a class is called
- A new instance of that class is created:
- The init method of the class is called with the new object as its first argument (named self), along with any additional arguments provided in the call expression
- What is self?
- references the object that you are trying to create
- technically binds self to what you bind the object to
- Object identity
- Every object that is an instance of a user-defined class has a unique identity
- Identity operators “is’ and “is not” test if two expressions evaluate to the same object
- Methods
- Methods
- Functions defined in the suite of a class statement
- self should always be bound to an instance of the Account class
- We need to know which account to apply the method on
- Invoking Methods
- All invoked methods have access to the object via the self parameter, and so they can all access and manipulate the object’s state
- Dot notation automatically supplies the first argument to a method
- tom_account.deposit(100)
- ^ “tom_account” technically being passed into self
- Dot expressions
- Objects receive messages via dot notation
- dot notation accesses attributes of the instance or its class
. can be any valid Python expression is probably more simple - Evaluates to the value of the attribute looked up by
in the object that is the value of the
- Attributes
- Accessing Attributes
- Using getattr, we can look up an attribute using a string
- getattr(tom_account, ‘balance’)
- passing in the object and the attribute you want to look up
- hasattr, searches if attribute exists
- Looking up an attribute name in an object may return
- one of its instance attributes
- one of the class attributes
- Methods and Functions
- Python distinguishes between:
- Functions
- Function: all arguments within parentheses
- Bound methods, which couple together a function and the object on which that method will be invoked
- Method: One object in the front and the rest of the args afte
- Object + Function = Bound Method
- Functions
- Looking up Attributes by name
- To evaluate a dot expression:
- Evaluate the
to the left of the dot, which yields the object of the dot expression is matched against the instance attributes of that object; if an attribute with that name exists, its value is returned - It not,
is looked up in the class, which yields a class attribute value - That value is returned unless it is a function, in which case a bound method is returned instead
- Evaluate the
- To evaluate a dot expression:
- Python distinguishes between:
- Class Attributes
- “shared’ across all instances of a class because they are attributes of the class, not he instance
- By convention, class attributes defined before the constructor
- Example: Bouncing Balls
- Ball Instance
- Allocate memory for a Ball object
- Initilialize the Ball object with values
- Return the Ball object
- Ball Class
- Making multiple Ball instances
- Multi-class programs
- Usually need more than one class of objects in our program to model its complexity
- Face, eye, nose
- All different classes
- The Class Statement