Object-Oriented Python

Similar documents
Lecture 17: Classes (Chapter 15)

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II

Lecture 21. Programming with Subclasses

Lecture 21. Programming with Subclasses

ECE 364 Software Engineering Tools Laboratory. Lecture 7 Python: Object Oriented Programming

Review 2. Classes and Subclasses

Before we start looking at how we build abstract data types in Python, let's define some import terms and look at some real-world examples.

A Crash Course in Python Part II. Presented by Cuauhtémoc Carbajal ITESM CEM

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Positional, keyword and default arguments

CSE : Python Programming. Decorators. Announcements. The decorator pattern. The decorator pattern. The decorator pattern

CS 11 python track: lecture 4

Object oriented programming

CSC148: Week 1

CIS192 Python Programming

Lecture 21. Programming with Subclasses

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

CS-202 Introduction to Object Oriented Programming

COMP519 Web Programming Lecture 21: Python (Part 5) Handouts

Programming I. Course 9 Introduction to programming

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

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

CIS192 Python Programming

Lecture 18. Classes and Types

Iterators & Generators

Software Development Python (Part B)

Introduction. We've seen Python useful for. This lecture discusses Object Oriented Programming. Simple scripts Module design

Object Oriented Programming in Python. Richard P. Muller Materials and Process Simulations Center California Institute of Technology June 1, 2000

61A Lecture 2. Friday, August 28, 2015

CSE : Python Programming

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19

A Little Python Part 3

CS 11 python track: lecture 2

Fundamentals of Programming. Week 11 - Lecture 1: OOP Part 2.

Effective Programming Practices for Economists

CSI33 Data Structures

Python A Technical Introduction. James Heliotis Rochester Institute of Technology December, 2009

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

Chapter 5 Object-Oriented Programming

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

OBJECT ORIENTED PROGRAMMING

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE

Data Structures (list, dictionary, tuples, sets, strings)

What is a Class? Short Answer. Responding to Messages. Everything in Python is an Object 11/8/2010

PREPARING FOR PRELIM 2

Lecture 3 - Overview. Object-oriented programming and classes Operators Very briefly about naming conventions Graphical user interfaces (GUIs)

Python OOP. Stéphane Vialette. LIGM, Université Paris-Est Marne-la-Vallée. October 19, 2010

Lecture 20. Subclasses & Inheritance

Week 2. Classes and Objects

Object Oriented Programming Enthought, Inc. 97

Question 1. tmp = Stack() # Transfer every item from stk onto tmp. while not stk.is_empty(): tmp.push(stk.pop())

Chapter 7. Inheritance

What is Inheritance?

Objects and Classes. You create a class object by executing a class statement, e.g.:

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

Lecture 15: Intro to object oriented programming (OOP); Linked lists

CMSC201 Computer Science I for Majors

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Lecture 19: Subclasses & Inheritance (Chapter 18)

CIS192 Python Programming

Harness the power of Python magic methods and lazy objects.

Python Crash-Course. C. Basso. Dipartimento di Informatica e Scienze dell Informazione Università di Genova. December 11, 2007

CSC326 Meta Programming i. CSC326 Meta Programming

CMSC201 Computer Science I for Majors

CIS192 Python Programming

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

CIS192 Python Programming

Exceptions. raise type(message) raise Exception(message)

Review 3. Exceptions and Try-Except Blocks

Recall. Key terms. Review. Encapsulation (by getters, setters, properties) OOP Features. CSC148 Intro. to Computer Science

What's New in Python 2.2

PC204 Lecture 8. Conrad Huang Genentech Hall, N453A x6-0415

CS200: Advanced OO in Java

Lecture 17: Classes (Chapter 15)

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

Descriptor HowTo Guide Release 3.3.3

PyTrie Documentation. Release 0.3. George Sakkis

CP215 Application Design

PREPARING FOR THE FINAL EXAM

At full speed with Python

CIS192 Python Programming

Python for Finance. Advanced Features. Andras Niedermayer

a declaration of class name, and a class docstring

CS61A Lecture 15 Object Oriented Programming, Mutable Data Structures. Jom Magrotker UC Berkeley EECS July 12, 2012

CS108 Lecture 16: User Defined Classes. Overview/Questions

Classes and Objects 1

COMPSCI 105 S Principles of Computer Science. Classes 3

TeenCoder : Java Programming (ISBN )

Dangerously Advanced Python


COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Outline. the try-except statement the try-finally statement. exceptions are classes raising exceptions defining exceptions

CPSC 217-T03/T08. Functions Ruting Zhou

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

PREPARING FOR THE FINAL EXAM

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

Fun with Python Descriptors

Lecture 5. Introduction to Python! Lecture 5

Transcription:

Object-Oriented Python

Everything is an object. Every object has a value. a type. an identity. a namespace. CS105 Python

def initstudent(student, eid): global A class is like a namespace. CS105 Python

def initstudent(student, eid): global A class is like Student a namespace. -- A class definition initstudent binds the class name to an object. These are the active namespaces after the class body. def initstudent(student, eid): CS105 Python 5

a def initstudent(student, eid): student = Student() Invoking a class name creates an instance of that class. -- An instance is like a namespace. These are the active namespaces after executing line a. Student initstudent global is an instance of def initstudent(student, eid): student CS105 Python 6

a b def initstudent(student, eid): student = Student() student.initstudent Accessing an instance s attributes causes chained lookups. These are the active namespaces when executing line b. Student initstudent global 1 is an instance of def initstudent(student, eid): student CS105 Python 7

a b explicit parameter def initstudent(self, eid): self.eid = eid self.courses = [] student = Student() student.initstudent('guido') implicit argument Methods are different from functions. These are the active namespaces just before executing line. Student eid initstudent global local 'guido' eid self is an instance of m def initstudent(student, eid): student CS105 Python 8

def init (self, eid): self.eid = eid self.courses = [] a student = Student('guido') Constructor creates and initializes at the same time. CS105 Python 10

def str (self): return eid Special Method Shortcut init Class() str str(obj) repr (Python representation) eq obj1 == obj getitem obj[i] setitem obj[i] = val Full list at docs.python.org/reference/datamodel.html#specialnames >>> student # calls repr < main.student instance at 0x8cb0> >>> print student # calls str guido CS105 Python 11

5 6 7 8 9 10 11 1 1 1 15 def init (self, eid): self.eid = eid self.courses = [] def enroll(self, course): self.courses.append(course) class Instructor: def init (self, eid): self.eid = eid self.courses = [] def assign(self, course): self.courses.append(course) Inheritance is one way to reuse code. CS105 Python 1

5 6 7 8 9 10 11 1 class Person: def init (self, eid): self.eid = eid self.courses = [] class Student(Person): def enroll(self, course): self.courses.append(course) class Instructor(Person): def assign(self, course): self.courses.append(course) Inheritance is one way to reuse code. CS105 Python 1

5 6 7 8 9 10 11 1 1 1 15 16 class Person: def init (self, eid): self.eid = eid self.courses = [] class Student(Person): def enroll(self, course): self.courses.append(course) class Instructor(Person): def init (self, eid, salary): Person. init (self, eid) self.salary = salary def assign(self, course): self.courses.append(course) Inheritance is one way to reuse code. overriding the base-class method explicitly invoking the base-class method CS105 Python 1

5 6 7 8 a b class Person: class Student(Person): class Instructor(Person): student = Student('guido') instructor = Instructor('wirth', 15000) Class Type >>> type(student) <type 'instance'> >>> type(instructor) <type 'instance'> CS105 Python 15

5 6 7 8 a b class Person(object): class Student(Person): class Instructor(Person): student = Student('guido') instructor = Instructor('wirth', 15000) Class Type >>> type(student) <class ' main.student'> >>> type(instructor) <class ' main.instructor'> CS105 Python 16

Subclassing Built-In Types class ReadOnlyList(list): def setitem (self, index, value): raise TypeError('This is a read-only list!') >>> rol = ReadOnlyList([1,,]) >>> rol[0] 1 >>> rol[0] = 0 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line, in setitem TypeError: This is a read-only list! CS105 Python 17

All a def init (self, eid): self.eid = eid self.courses = [] student = Student('guido') data is public. >>> student.eid 'guido' >>> dir(student) [' doc ', ' init ', ' module ', 'courses', 'eid'] CS105 Python 18

You a def init (self, eid): self. eid = eid self. courses = [] student = Student('guido') can hide it, but >>> student. eid Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: Student instance has no attribute ' eid' CS105 Python 19

All a def init (self, eid): self. eid = eid self. courses = [] student = Student('guido') data is public. >>> dir(student) ['_Student courses', '_Student eid', ' doc ', ' init ', ' module '] >>> student._student eid 'guido' CS105 Python 0

Encapsulation By Social Contract signal: _name protect: name The underscore says, This is an implementation detail. Modify at your own peril. CS105 Python 1