Object-Oriented Programming

Similar documents
61A LECTURE 12 OOP 2, INHERITANCE. Steven Tang and Eric Tzeng July 14, 2013

Announcements. Homework 1 due Monday 10/12 (today) Homework 2 released next Monday 10/19 is due 11/2

61A Lecture 16. Wednesday, October 5

Ch.7: Introduction to classes (part 2)

CS61B Lecture #7. Announcements:

Object Oriented Programming

Object Oriented Programming

Announcements. Last modified: Fri Sep 8 00:59: CS61B: Lecture #7 1

CS61A Lecture 21. Amir Kamil UC Berkeley March 11, 2013

Computational Concepts Toolbox. Object Oriented Programming. Today: class. Review: Objects

Object Oriented Programming. Feb 2015

61A LECTURE 17 ORDERS OF GROWTH, EXCEPTIONS. Steven Tang and Eric Tzeng July 23, 2013

Introduction to Object-Oriented Programming

Lecture 2: Writing Your Own Class Definition

CS 112 Intro to Programming Basic OO Concepts

Python Language Basics II. Georgia Advanced Computing Resource Center University of Georgia Zhuofei Hou, HPC Trainer

Outline. Introduction to Python. ABAQUS Scripting (an example)

Berkeley Scheme s OOP

Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors

CS61A Lecture 18. Amir Kamil UC Berkeley March 4, 2013

Structure and Interpretation of Computer Programs Fall 2014 Midterm 2

Structure and Interpretation of Computer Programs Fall 2012 Alternate Midterm 2

Object-Oriented Programming

Online Account Aggregation (Member-to-Member Transfers)

CS 617 Object Oriented Systems Lecture 5 Classes, Classless World:Prototypes, Instance Variables, Class Variables, This/Self 3:30-5:00 pm Thu, Jan 17

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

Structure and Interpretation of Computer Programs

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

Objects and Classes. Chapter 8

CONCURRENCY AND MAPREDUCE 15

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

Class definition. F21SC Industrial Programming: Python. Post-facto setting of class attributes. Class attributes

F21SC Industrial Programming: Python: Classes and Exceptions

Programming I. Course 9 Introduction to programming

61A Lecture 2. Wednesday, September 4, 2013

CS 315 Programming Languages Spring 2016 Homework 1 DUE: Mar 5, :59

Algorithms and Programming

CPS122 Lecture: Defining a Class

Working with Lists 4

61A LECTURE 10 MUTABLE DATA. Steven Tang and Eric Tzeng July 10, 2013

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

1.00 Lecture 8. Using An Existing Class, cont.

CS108 Lecture 16: User Defined Classes. Overview/Questions

Pairs and Lists. (cons 1 2) 1 2. (cons 2 nil) 2 nil. Not a well-formed list! 1 > (cdr x) 2 > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) ( ) (Demo)

return total def cube(k): return pow(k, 3) >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n:

String Representations

CS61B Lecture #5: Arrays and Objects

Object Oriented Programming in Python 3

Lecture #15: OOP. A means of defining and using operations specific to these objects.

Inheritance in Ruby. You are familiar with the idea of inheritance and how to use this in programming.

61A LECTURE 8 SEQUENCES, ITERABLES

61A LECTURE 8 SEQUENCES, ITERABLES. Steven Tang and Eric Tzeng July 8, 2013

CGS 2405 Advanced Programming with C++ Course Justification

61A Lecture 2. Friday, August 28, 2015

University of Swaziland Department Of Computer Science Supplementary Examination JULY 2012

CS 115 Lecture 21. Classes, data structures, and C++ Neil Moore

Final Exam Solutions. Structure and Interpretation of Computer Programs. Fall 2011 /12 /16 /12 /18 /10 /12 /80 INSTRUCTIONS

Objectives: 1. Introduce the course. 2. Define programming 3. Introduce fundamental concepts of OO: object, class

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

Streams and Evalutation Strategies

1 State, objects, and abstraction

Basic Object-Oriented Concepts. 5-Oct-17

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Inheritance. A mechanism for specialization A mechanism for reuse. Fundamental to supporting polymorphism

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

61A Lecture 3. Friday, September 5

Python List built-in methods, accessing elements, selection, slicing, recursive functions, list comprehension

CS1210 Lecture 28 Mar. 27, 2019

What is a class? Responding to messages. Short answer 7/19/2017. Code Listing 11.1 First Class. chapter 11. Introduction to Classes

Objects and Classes -- Introduction

CSC102 INTRO TO PROGRAMMING WITH PYTHON REVIEW MICHAEL GROSSBERG

The PCAT Programming Language Reference Manual

61A Lecture 4. Monday, September 9

JAVA: A Primer. By: Amrita Rajagopal

Friday, 11 April 14. Advanced methods for creating decorators Graham Dumpleton PyCon US - April 2014

CS61A Lecture 20 Object Oriented Programming: Implementation. Jom Magrotker UC Berkeley EECS July 23, 2012

STUDENT LESSON A5 Designing and Using Classes

Practice problem on defining and using Class types. Part 4.

Introduction to Unified Modelling Language (UML)

Functional programming in LISP

Intermediate Cucumber. CSCI 5828: Foundations of Software Engineering Lecture 17 03/13/2012

Software Design and Analysis for Engineers

CS558 Programming Languages Winter 2013 Lecture 8

1st Semester MTCE 601A COMPUTER SYSTEM SOFTWARE

Object Oriented Programming with c++ Question Bank

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Functions Structure and Parameters behind the scenes with diagrams!

Internet Application Developer

ECE 122. Engineering Problem Solving with Java

Introduction to Computers and Programming Languages. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Object Oriented Programming 2012

AC55/AT55 OBJECT ORIENTED PROGRAMMING WITH C++ DEC 2013

Introduction to Programming Using Python Lecture 4. Dr. Zhang COSC 1437 Fall, 2018 October 11, 2018

Stateful Objects. can I withdraw 100 CHF? may vary over the course of the lifetime of the account.

Unit Title: Objects in Visual Basic.NET. Software Development Unit 4. Objects in Visual Basic.NET

KCCU Online Banking - For Members Use

Packages & Random and Math Classes

SCHEME AND CALCULATOR 5b

User Defined Types. Babes-Bolyai University Lecture 06. Lect Phd. Arthur Molnar. User defined types. Python scope and namespace

Example: Class MSDie Introduction to Graphs and NetworkX

Transcription:

61A Lecture 15

Announcements

Object-Oriented Programming

Object-Oriented Programming A method for organizing programs Data abstraction Bundling together information and related behavior John's Account Apply for a loan! A metaphor for 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 Several objects may all be instances of a common type Different types may relate to each other Withdraw $10 Deposit $10 Jack's Account John Specialized syntax & vocabulary to support this metaphor 4

Classes A class serves as a template for its instances Idea: All bank accounts have a balance and an account holder; the Account class should add those attributes to 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 method and a deposit method >>> a = Account('John') >>> a.holder 'John' >>> a.balance 0 >>> a.deposit(15) 15 >>> a.withdraw(10) 5 >>> a.balance 5 >>> a.withdraw(10) 'Insufficient funds' 5

Class Statements

The Class Statement class <name>: <suite> The suite is executed when the class statement is executed. A class statement creates a new class and binds that class to <name> in the first frame of the current environment Assignment & def statements in <suite> create attributes of the class (not names in frames) >>> class Clown:... nose = 'big and red'... def dance():... return 'No thanks'... >>> Clown.nose 'big and red' >>> Clown.dance() 'No thanks' >>> Clown <class ' main.clown'> 7

Object Construction Idea: All bank accounts have a balance and an account holder; the Account class should add those attributes to each of its instances >>> a = Account('Jim') >>> a.holder 'Jim' >>> a.balance 0 When a class is called: 1.A new instance of that class is created: An account instance balance: 0 holder: 'Jim' 2.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 init is called a constructor class Account: def init (self, account_holder): self.balance = 0 self.holder = account_holder 8

Object Identity Every object that is an instance of a user-defined class has a unique identity: >>> a = Account('John') >>> b = Account('Jack') >>> a.balance 0 >>> b.holder 'Jack' Every call to Account creates a new Account instance. There is only one Account class. Identity operators "is" and "is not" test if two expressions evaluate to the same object: >>> a is a True >>> a is not b True Binding an object to a new name using assignment does not create a new object: >>> c = a >>> c is a True 9

Methods

Methods Methods are functions defined in the suite of a class statement class Account: def init (self, account_holder): self.balance = 0 self.holder = account_holder self should always be bound to an instance of the Account class def deposit(self, amount): self.balance = self.balance + amount return self.balance def withdraw(self, amount): if amount > self.balance: return 'Insufficient funds' self.balance = self.balance - amount return self.balance These def statements create function objects as always, but their names are bound as attributes of the classs 11

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 class Account: Defined with two parameters... def deposit(self, amount): self.balance = self.balance + amount return self.balance Dot notation automatically supplies the first argument to a method >>> tom_account = Account('Tom') >>> tom_account.deposit(100) 100 Bound to self Invoked with one argument 12

Dot Expressions Objects receive messages via dot notation Dot notation accesses attributes of the instance or its class <expression>. <name> The <expression> can be any valid Python expression The <name> must be a simple name Evaluates to the value of the attribute looked up by <name> in the object that is the value of the <expression> tom_account.deposit(10) Dot expression Call expression (Demo) 13

Attributes (Demo)

Accessing Attributes Using getattr, we can look up an attribute using a string >>> getattr(tom_account, 'balance') 10 >>> hasattr(tom_account, 'deposit') True getattr and dot expressions look up a name in the same way Looking up an attribute name in an object may return: One of its instance attributes, or One of the attributes of its class 15

Methods and Functions Python distinguishes between: Functions, which we have been creating since the beginning of the course, and Bound methods, which couple together a function and the object on which that method will be invoked Object + Function = Bound Method >>> type(account.deposit) <class 'function'> >>> type(tom_account.deposit) <class 'method'> >>> Account.deposit(tom_account, 1001) 1011 >>> tom_account.deposit(1004) 2015 Function: all arguments within parentheses Method: One object before the dot and other arguments within parentheses 16

Looking Up Attributes by Name <expression>. <name> 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 value 4. That value is returned unless it is a function, in which case a bound method is returned instead 17

Class Attributes Class attributes are "shared" across all instances of a class because they are attributes of the class, not the instance class Account: interest = 0.02 # A class attribute def init (self, account_holder): self.balance = 0 self.holder = account_holder # Additional methods would be defined here >>> tom_account = Account('Tom') >>> jim_account = Account('Jim') >>> tom_account.interest 0.02 >>> jim_account.interest 0.02 The interest attribute is not part of the instance; it's part of the class! 18