Class 25: Python, Objects, Bombs, and Inheritance University of Virginia cs1120 David Evans.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

Python Objects and Classes
Introduction to OCaml Slides prepared by Matt Gruskin Some material borrowed from the CIS 500 lecture notes.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Class 21: Imperative Programming University of Virginia cs1120 David Evans.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Inheritance.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 16: Quicker Sorting.
Fundamentals of Programming II Inheritance and Abstract Classes
INFO 206 Lab Exercise 1 Introduction to Classes and Objects 1/18/2012i206 Lab 1 - Exercise1.
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
Class 24: Programming with Objects University of Virginia cs1120 David Evans.
Guide to Programming with Python
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
UVa cs1120 David Evans Lecture 34: Djustifying Django.
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Programming Languages and Paradigms Object-Oriented Programming.
Inheritance. Inhertance Inheritance is used to indicate that one class will get most or all of its features from a parent class. class Dog(Pet): Make.
Chapter 10 Classes. Review of basic OOP concepts  Objects: comprised of associated DATA and ACTIONS (methods) which manipulate that data.  Instance:
11/27/07. >>> Overview * objects * class * self * in-object methods * nice printing * privacy * property * static vs. dynamic * inheritance.
By: Chris Harvey Python Classes. Namespaces A mapping from names to objects Different namespaces have different mappings Namespaces have varying lifetimes.
What is MOF? The Meta Object Facility (MOF) specification provides a set of CORBA interfaces that can be used to define and manipulate a set of interoperable.
CS61A Lecture 15 Object-Oriented Programming, Mutable Data Structures Jom Magrotker UC Berkeley EECS July 12, 2012.
An Introduction to Python Blake Brogdon. What is Python?  Python is an interpreted, interactive, object-oriented programming language. (from python.org)
Built-in Data Structures in Python An Introduction.
Object-Oriented Design Justin Callanan CS 597. Object-Oriented Basics ● Data is stored and processed in “objects” ● Objects represent generalized real.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 30: Laziness.
Guide to Programming with Python Week 11 Chapter Nine Inheritance Working with multiple objects.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Introduction to OOP OOP = Object-Oriented Programming OOP is very simple in python but also powerful What is an object? data structure, and functions (methods)
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 3: Levels of Abstraction
P YTHON ’ S C LASSES Ian Wynyard. I NTRODUCTION TO C LASSES A class is the scope in which code is executed A class contains objects and functions that.
CSC 131 Fall 2006 Lecture # 6 Object-Oriented Concepts.
Lists, Tuples, Dictionaries, … + lots of computer work for the programmer's work! T = {'abe' :['homer','herb'], 'jackie':['marge','patty','selma'], 'homer'
PYTHON OBJECTS & CLASSES. What is an object? The abstract idea of anything What is in an object: Attributes Characteristics Represented by internal variables.
Chapter Object Oriented Programming (OOP) CSC1310 Fall 2009.
Cs2220: Engineering Software Class 3: Java Semantics Fall 2010 University of Virginia David Evans.
Object Oriented Programing (OOP)
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University,
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 23: Programming with Objects.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Cs1120 Fall 2009 David Evans Lecture 18: Changing State Sounds of Colossus:
A Tutorial on the Python Programming Language. Overview Running Python and Output Data Types Input and File I/O Control Flow Functions.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 6: Ordered Data Abstractions
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 22: Objectifying Objects.
Python Basics 본 자료는 다음의 웹 사이트를 정리 한 내용이니 참조 바랍니다. ythonBasics.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Xi Wang Yang Zhang. 1. Strings 2. Text Files 3. Exceptions 4. Classes  Refer to basics 2.py for the example codes for this section.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 21: Inheritance.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
2-1 © Prentice Hall, 2004 Chapter 2: Introduction to Object Orientation Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph.
CMSC201 Computer Science I for Majors Lecture 18 – Classes and Modules (Continued, Part 3) Prof. Katherine Gibson Based on slides from the.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Example: Vehicles What attributes do objects of Sedan have?
CS-104 Final Exam Review Victor Norman.
Lecture VI Objects The OOP Concept Defining Classes Methods
Lecture 21: Inheritance CS200: Computer Science University of Virginia
Basic Inheritance Damian Gordon.
Lecture 4: Data Abstraction CS201j: Engineering Software
Objects (again) Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Presentation transcript:

Class 25: Python, Objects, Bombs, and Inheritance University of Virginia cs1120 David Evans

Menu PS5: end-auction! running time Lists in Python Inheritance

end-auction! (define (end-auction!) (mmap (lambda (item-entry) (let ((item-name (list-get-element item-entry (table-field-number items 'item-name)))) (let ((high-bid (get-highest-bid item-name))) (if (null? high-bid) (printf "No bids on ~a.~n“ (list-get-element item-entry (table-field-number items 'item-name))) (printf "Congratulations ~a! You have won the ~a for $~a.~n" (list-get-element high-bid (table-field-number bids 'bidder-name)) item-name (list-get-element high-bid (table-field-number bids 'amount))) (list-get-element high-bid (table-field-number bids 'amount)))))) (table-entries items)))) mmap: N applications of mapping procedure list-get-element running time is in  (N) get-highest-bid running time is in  (N) Overall running time: N * (  (N) +  (N)) =  (N 2 ) mmap: N applications of mapping procedure list-get-element running time is in  (N) get-highest-bid running time is in  (N) Overall running time: N * (  (N) +  (N)) =  (N 2 ) WRONG: need to be careful what N means! WRONG: need to be careful what N means!

end-auction! (define (end-auction!) (mmap (lambda (item-entry) (let ((item-name (list-get-element item-entry (table-field-number items 'item-name)))) (let ((high-bid (get-highest-bid item-name))) (if (null? high-bid) (printf "No bids on ~a.~n“ (list-get-element item-entry (table-field-number items 'item-name))) (printf "Congratulations ~a! You have won the ~a for $~a.~n" (list-get-element high-bid (table-field-number bids 'bidder-name)) item-name (list-get-element high-bid (table-field-number bids 'amount))) (list-get-element high-bid (table-field-number bids 'amount)))))) (table-entries items)))) mmap: t applications of mapping procedure t is the number of entries in the items table list-get-element running time is in  (N) => constant time N is the number of elements in input list: here, number of fields in items: constant get-highest-bid running time is in  (N) =>  (b) N is number of entries in bids table, b Overall running time: t * (O(1) +  (b)) =  (tb) where t is number of items, b is number of bids mmap: t applications of mapping procedure t is the number of entries in the items table list-get-element running time is in  (N) => constant time N is the number of elements in input list: here, number of fields in items: constant get-highest-bid running time is in  (N) =>  (b) N is number of entries in bids table, b Overall running time: t * (O(1) +  (b)) =  (tb) where t is number of items, b is number of bids

Python Lists Built-in datatypes for both mutable lists [] and immutable tuples () >>> m = range(1, 1000) >>> m[0] 1 >>> m[-1] 999 >>> len(m) 999 >>> m[1:] [2,..., 999] range(1,1000)  (intsto 999) m[0]  (mcar m) Python lists can access any element in approx. constant time! m[1:]  (mcdr m) ? len is also constant time

Is m[1:] like mcdr? >>> m1 = m[1:] >>> m1[0] 2 >>> m[1] 2 >>> m1[0] = 3 >>> m[1] 2 m[1:] is a new copy of the list, except for the first element. Uses  (N) time and space! m[1:] is a new copy of the list, except for the first element. Uses  (N) time and space!

Implementing list-map in Python def schemish_list_map(f, p): if not p: return [] else: return [f(p[0])] + schemish_list_map(f, p[1:]) def schemish_list_map(f, p): if not p: return [] else: return [f(p[0])] + schemish_list_map(f, p[1:]) “Literal” translation...not a good way to do this. Running time is in  (N 2 ) where N is number of elements in p. Note: there is a built-in map in Python.

Pythonic Mapping def mlist_map(f, p): for i in range(0, len(p)): p[i] = f(p[i]) return p Unlike the previous one, this mutates p.

Inheritance

There are many kinds of Dogs… class Dog: def __init__(self, n): self.name = n def bark(self): print “wuff wuff wuff wuff” class TalkingDog (Dog): def speak(self, stuff): print stuff

Subclasses ClassDefinition ::= class SubClassName ( SuperClassName ) : FunctionDefinitions class TalkingDog (Dog): def speak(self, stuff): print stuff TalkingDog is a subclass of Dog. Dog is the superclass of TalkingDog.

Every Dog has its Day >>> bo = Dog('Bo') >>> scooby = TalkingDog('Scooby Doo') >>> scooby.speak('Ta-da!') Ta-da! >>> bo.speak('Ta-da!') Traceback (most recent call last): File " ", line 1, in bo.speak('Ta-da!') AttributeError: Dog instance has no attribute 'speak‘ >>> scooby.bark() wuff wuff class Dog: def __init__(self, n): self.name = n def bark(self): print “wuff wuff wuff wuff” class Dog: def __init__(self, n): self.name = n def bark(self): print “wuff wuff wuff wuff” class TalkingDog (Dog): def speak(self, stuff): print stuff class TalkingDog (Dog): def speak(self, stuff): print stuff

Speaking about Inheritance Inheritance is using the definition of one class to define another class. TalkingDog inherits from Dog. TalkingDog is a subclass of Dog. The superclass of TalkingDog is Dog. Dog TalkingDog These all mean the same thing.

PS6 Make an adventure game programming with objects Many objects in our game have similar properties and behaviors, so we use inheritance.

PS6 Classes SimObject PhysicalObject Place MobileObject OwnableObject Person Student PoliceOfficer

SimObject PhysicalObject Place MobileObject OwnableObject Person Student PoliceOfficer class SimObject: def __init__(self, name): self.name = name def note(self, msg): print "%s: %s" % (self, msg) class PhysicalObject (SimObject): def __init__(self, name): SimObject.__init__(self, name) self.location = None def install(self, loc): self.note ("Installing at " + str(loc)) self.location = loc loc.add_thing(self) class MobileObject (PhysicalObject): def change_location(self, loc): self.location.remove_thing(self) loc.add_thing(self) self.location = loc

SimObject PhysicalObject Place MobileObject OwnableObject Person Student PoliceOfficer class MobileObject (PhysicalObject): def change_location(self, loc): self.location.remove_thing(self) loc.add_thing(self) self.location = loc class OwnableObject (MobileObject): def __init__(self, name): MobileObject.__init__(self, name) self.owner = None def is_ownable(self): return True

SimObject PhysicalObject Place MobileObject OwnableObject Person Student PoliceOfficer PS6 Objects Place(‘Cabal Hall’) aph = Student(‘Alyssa P. Hacker’) An object that is an instance of the Place class.

Object-Oriented Summary An object packages state and procedures. A class provides procedures for making and manipulating a type of object. The procedures for manipulating objects are called methods. We invoke a method on an object. Inheritance allows one class to refine and reuse the behavior of another. This is a good thing. Friday: Excursion on Exponential Growth – Please ready Tyson essay before Friday!