Object-Oriented Programming
Why OOP?
- A (hopefully) more intuitive way of representing data
- A common method for organizing programs
- Formally split "global state" and "local state" for every object
Classes
- Every object is an instance of a class
- A class is a type or category of objects
- A class serves as a blueprint for its instances
The Class Statement
: Class Statements defines the blueprint
Assignment & def statements in the Class Statement create class attributes
Before we start implementing our methods, we need to talk about how to create Accounts
Idea: All bank accounts have a balance and an account holder. These are not shared across Accounts.
When a class is called :
1. A new instance of that class is created.
2. The __init__ method of a class is called with the new object as its first argument (named self), along with additional arguments provided in the call expression.
Terminology : Attributes, Functions, and Methods
- All objects have attributes, which are 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
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 (the arrows point to the same place)
: Binding an object to a new name using assignment does not create a new object
Dot Expressions
: You can access class or instance attributes with dot notation.
: The <expression> can be any valid Python expression that evaluates to a class or instance. The <name> must be an attribute or a method.
To evaluate a dot expression:
1. Evaluate the <expression> to the left of the dot, which yields the object of the dot expression
2. <name> is matched against the instance attributes of that object; if an attribute with that name exists, its value is returned
3. If not, <name> is looked up in the class, which yields a class attribute or a method
4. The corresponding attribute is returned or corresponding method is called
Methods and Functions
: Methods are functions defined in the suite of a class statement
However methods that are accessed through an instance will be bound methods. Bound methods couple together a function and the object on which that method will be invoked. This means that when we invoke bound methods, the instance is automatically passed in as the first argument.
Invoking Methods
We can call class methods in two ways: as a bound method and as a function.
Invoking class methods as a bound method:
- Bound methods are accessed through the instance and implicitly pass the instance object in as the first argument of the method
- <instance>.<method_name>(<arguments>)
- a.deposit(5)
Invoking class methods as functions:
- We can use the class name to directly call a method. These follow our typical function call rules and nothing is implicitly passed in.
- <class_name>.<method_name>(<instance>, <arguments>)
- Account.deposit(a, 5)
Implementing the Account class
Accessing Attributes
There are built-in functions that can help us access attributes.
: Using getattr, we can look up an attribute using a string instead.
: Using hasattr, we can check if an attribute exists.
Assigning Attributes
< expression > . < name > = < value >
Change attributes for the object of that dot expression.
If the expression evaluates to an instance : then assignment sets an instance attribute, even if it exists in the class
If the expression evaluates to a class : then assignment sets a class attribute
'University of California, Berkeley > ElectricalEngineering & ComputerSciences' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Linked Lists & Mutable Trees (0) | 2019.07.26 |
---|---|
Inheritance (0) | 2019.07.24 |
Week 5 (0) | 2019.07.23 |
(Higher-Order) Functions, Sequences, Recursion and TreeRecursion, Trees (0) | 2019.07.18 |
Iterators & Generators (0) | 2019.07.17 |