Object-oriented Programming
Object-Oriented Programming
Why OOP?
Why OOP?
Object-Oriented Programming A(hopefully)more intuitive way of representing data A common method for organizing programs Formally split"global state"and "local state"for every object
Object-Oriented Programming ● 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 Instance Tiffany:is a Human Class Tiffany has a :name and an:age Attributes Tiffany can:talk:to other Humans Behaviors
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 Tiffany is a Human Tiffany has a name and an age Instance Class Attributes Tiffany can talk to other Humans Behaviors
Example:Bank Accounts A class serves as a blueprint for its instances Tiff's Idea:All bank accounts have a balance Account and an account holder; the Account class should define those values for each newly created instance. Withdraw S5 Idea:All bank accounts should have Transfer $5 "withdraw"and "deposit"behaviors that all work in the same way. Alex's Better Idea:All bank accounts share a Account "withdraw"and "deposit"method. Deposit $5 All accounts might also share other characteristics like maximum withdrawal, or loan limits
Example: Bank Accounts Tiff’s Account Alex's Account Deposit $5 Withdraw $5 Transfer $5 A class serves as a blueprint for its instances Idea: All bank accounts have a balance and an account holder; the Account class should define those values for each newly created instance. Idea: All bank accounts should have “withdraw” and “deposit” behaviors that all work in the same way. Better Idea: All bank accounts share a “withdraw” and “deposit” method. All accounts might also share other characteristics like maximum withdrawal, or loan limits
The Class Statement Class Statements defines the blueprint Assignment def statements in the Class Statement create class class : attributes. Lots of...don't worry! Global frame Account class Account: Tables are not frames max_withdrawal 10 Account class def deposit(...): max_withdrawal 10 func deposit(...) 年 [p=G] def withdraw(.·.) deposit Class attribute func withdraw withdraw(...) [p=G] Bounds methods
The Class Statement class Account: max_withdrawal = 10 def deposit(...): ... def withdraw(...): ... ... class : Class Statements defines the blueprint Assignment & def statements in the Class Statement create class attributes. Global frame Account Account class func withdraw(...) [p=G] func deposit(...) [p=G] Tables are not frames Lots of … don’t worry! max_withdrawal 10 deposit withdraw Class attribute Bounds methods
The Class Statement 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. _init_is called class Account: the constructor ··。 def __init__(self,account_holder): self.balance 0 self.holder account_holder
The Class Statement 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. class Account: ... def __init__(self, account_holder): self.balance = 0 self.holder = account_holder ... __init__ is called the constructor
The Class Statement Global frame Account instance class Account: Account balance 0 max_withdrawal 10 holder “Tiff" def __init__(self,account_holder): self.balance =0 Instance self.holder account_holder attributes Account class def deposit(self,amount): max_withdrawal 10 func __init__(..\) Class attribute __init__ [p=G] def withdraw(self,amount): deposit func deposit(...) withdraw [p=G] >>a Account('Tiff') func withdraw(...) f1:init [p=G] [p=G] Python Tutor link self account_holder "Tiff" RV None
The Class Statement Global frame Account Account class func deposit(...) [p=G] func __init__(...) [p=G] func withdraw(...) [p=G] Account instance f1: __init__ [p=G] self account_holder RV "Tiff" None a max_withdrawal 10 __init__ deposit withdraw balance 0 holder “Tiff” class Account: max_withdrawal = 10 def __init__(self, account_holder): self.balance = 0 self.holder = account_holder def deposit(self, amount): ... def withdraw(self, amount): ... >>> a = Account('Tiff') Instance attributes Class attribute Python Tutor link
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
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 Python Tutor link 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
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 Python Tutor link