CS 11 python track: lecture 4

Similar documents
CS 11 python track: lecture 2

CS 11 python track: lecture 3. n Today: Useful coding idioms

Review 3. Exceptions and Try-Except Blocks

CSE : Python Programming

PREPARING FOR PRELIM 2

INHERITANCE AND INTERFACES 7

CIS192 Python Programming

Agenda. Excep,ons Object oriented Python Library demo: xml rpc

Class extension and. Exception handling. Genome 559

Class extension and. Exception handling. Genome 559

CS 11 java track: lecture 3

CIS192 Python Programming

PREPARING FOR THE FINAL EXAM

CIS192 Python Programming

OBJECT ORIENTED PROGRAMMING

Python for Finance. Advanced Features. Andras Niedermayer

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

CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods. Dan Grossman Autumn 2018

Inheritance and Polymorphism. CS180 Fall 2007

CS1 Lecture 27 Mar. 26, 2018

Today. CSE341: Programming Languages. Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins. Ruby instance variables and methods

Review 2. Classes and Subclasses

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

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

Lecture 19: Subclasses & Inheritance (Chapter 18)

CMSC201 Computer Science I for Majors

14. Exception Handling

CSC148: Week 2

Absent: Lecture 3 Page 1. def foo(a, b): a = 5 b[0] = 99

Admin. CS 112 Introduction to Programming. Recap: OOP Analysis. Software Design and Reuse. Recap: OOP Analysis. Inheritance

Advanced topics, part 2

Programming I. Course 9 Introduction to programming

CSc 110, Spring 2017 Lecture 37: Critters. Adapted from slides by Marty Stepp and Stuart Reges

Week 2. Classes and Objects

Lecture 21. Programming with Subclasses

CS-202 Introduction to Object Oriented Programming

Lecture 21. Programming with Subclasses

Exception Handling. Genome 559

Overview. Elements of Programming Languages. Objects. Self-Reference

Building custom components IAT351

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

Exceptions and File I/O

LECTURE 6 Python Basics Part 5

2.6 Error, exception and event handling

CIS192 Python Programming

Arrays Classes & Methods, Inheritance

Week 8 Lecture: Programming

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

Overriding Variables: Shadowing

Lecture 18. Classes and Types

Lecture 38: Python. CS 51G Spring 2018 Kim Bruce

QUESTIONS FOR AVERAGE BLOOMERS

Object Oriented Programming

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

CSC148 Intro. to Computer Science

Object-Oriented Python

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Object Model Comparisons

Rules and syntax for inheritance. The boring stuff

Inheritance and Polymorphism

Overview. Elements of Programming Languages. Objects. Self-Reference

Lecture #12: Quick: Exceptions and SQL

CSE : Python Programming

Introduction to Python programming, II

COMP 110/L Lecture 19. Kyle Dewey

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

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Lecture 20. Subclasses & Inheritance

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

CS 251 Intermediate Programming Inheritance

Exceptions & a Taste of Declarative Programming in SQL

What is Inheritance?

Data Abstraction. Hwansoo Han

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 14 Object Oriented Programming TODAY 7/11/2012 WHERE WE WERE: FUNCTIONAL PROGRAMMING

Intro. Classes & Inheritance

C++ Modern and Lucid C++ for Professional Programmers

Exception Handling: Control. Exception handling is the control of error conditions or other unusual events during the execution of a program.

CSc 110, Spring 2017 Lecture 38: Critters. Adapted from slides by Marty Stepp and Stuart Reges

MITOCW watch?v=flgjisf3l78

Python in 10 (50) minutes

SCHEME INTERPRETER GUIDE 4

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

OBJECT ORIENTED PROGRAMMING 6

Software Development Python (Part B)

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

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

Java. Error, Exception, and Event Handling. Error, exception and event handling. Error and exception handling in Java

Making New instances of Classes

CSC148H Week 1 >> COURSE INFORMATION, RECAP, OOP. Sadia Rain Sharmin Week of May 7, 2018

CSE 341: Programming Languages. Section AC with Nate Yazdani

Python for Astronomers. Errors and Exceptions

Try and Error. Python debugging and beautification

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

Chapter 7. Inheritance

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Lecture Notes on Programming Languages

Smalltalk: developed at Xerox Palo Alto Research Center by the Learning Research Group in the 1970 s (Smalltalk-72, Smalltalk-76, Smalltalk-80)

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

PREPARING FOR THE FINAL EXAM

PROGRAMMING LANGUAGE 2

Transcription:

CS 11 python track: lecture 4 Today: More odds and ends assertions "print >>" syntax more on argument lists functional programming tools list comprehensions More on exception handling More on object-oriented programming inheritance, multiple inheritance, etc.

Odds and ends (1) Assertions # 'i' should be zero here: assert i == 0 # If fail, exception raised. "print to" syntax import sys print >> sys.stderr, "bad!"

Note on error messages Error messages should always go to sys.stderr Two ways to do this: import sys print >> sys.stderr, "bad!" sys.stderr.write("bad!\n") Either is fine Note that write() doesn't add newline at end

Odds and ends (2) arg lists Default arguments, keyword arguments def foo(val=10): print val foo() # prints 10 foo(20) # prints 20 foo(val=30) # prints 30 Default args must be at end of argument list

Odds and ends (3) arg lists Arbitrary number of arguments def foo(x, y, *rest): print x, y # print tuple of the rest args: print rest >>> foo(1, 2, 3, 4, 5) 1 2 (3, 4, 5)

Odds and ends (4) arg lists Keyword args: def foo(x, y, **kw): print x, y print kw >>> foo(1, 2, bar=6, baz=7) 1 2 { 'baz' : 7, 'bar' : 6 }

Odds and ends (4) arg lists Arbitrary number of args + keyword args: def foo(x, y, *rest, **kw): print x, y print rest print kw >>> foo(1, 2, 3, 4, 5, bar=6, baz=7) 1 2 (3, 4, 5) { baz : 7, bar : 6 }

Functional programming tools (1) First-class functions: def foo(x): return x * 2 >>> bar = foo >>> bar(3) 6

Functional programming tools (2) lambda, map, reduce, filter: >>> map(lambda x: x * 2, [1, 2, 3, 4, 5]) [2, 4, 6, 8, 10] >>> reduce(lambda x, y: x + y, [1, 2, 3, 4, 5]) 15 >>> sum([1, 2, 3, 4, 5]) # easier 15 >>> filter(lambda x: x % 2 == 1, range(10)) [1, 3, 5, 7, 9]

List comprehensions >>> vec = [2, 4, 6] >>> [3 * x for x in vec] [6, 12, 18] >>> [3 * x for x in vec if x > 3] [12, 18] >>> [3 * x for x in vec if x < 2] [] >>> [[x, x**2] for x in vec] [[2, 4], [4, 16], [6, 36]]

try/finally (1) We put code that can raise exceptions into a try block We catch exceptions inside except blocks We don't have to catch all exceptions If we don't catch an exception, it will leave the function and go to the function that called that function, until it finds a matching except block or reaches the top level Sometimes, we need to do something regardless of whether or not an exception gets thrown e.g. closing a file that was opened in a try block

try/finally (2) try: # code goes here... if something_bad_happens(): raise MyException("bad") finally: # executes if MyException was not raised # executes and re-raises exception # if MyException was raised

try/finally (3) Typical example of try/finally use: try: myfile = file("foo") # open file "foo" if something_bad_happens(): raise MyException("bad") finally: # Close the file whether or not an # exception was thrown. myfile.close() # If an exception was raised, python # will automatically reraise it here.

try/finally (4) Execution profile: try: #... code (1)... finally: #... code (2)... #... code (3)... When no exception raised: (1) then (2) then (3) When exception raised: (1) then (2) then exit function

try/finally (5) This is also legal: try: # code that can raise exceptions except SomeException, e: # code to handle exceptions finally: # code to execute whether or not # an exception was raised

try/finally (6) try: #... code (1)... except: #... code (2)... finally: #... code (3)... #... code (4)... When no exception raised: (1) then (3) then (4) When exception raised and caught: (1) then (2) then (3) then (4) When exception raised but not caught: (1) then (3) then exit function

Exception classes Exception classes, with arguments: class MyException: def init (self, value): self.value = value def str (self): return str(self.value) try: raise MyException(42) except MyException, e: print "bad! value: %d" % e.value

More on OOP -- inheritance Often want to create a class which is a specialization of a previously-existing class Don't want to redefine the entire class from scratch Just want to add a few new methods and fields To do this, the new class can inherit from another class; this is called inheritance The class being inherited from is called the parent class, base class, or superclass The class inheriting is called the child class, derived class, or subclass

Inheritance (2) Inheritance: class SubClass(SuperClass): <statement-1>... <statement-n> Or: class SubClass(mod.SuperClass): #... if SubClass is defined in another module

Inheritance (3) Name resolution: foo = Foo() # instance of class Foo foo.bar() If bar method not in class Foo superclass of Foo searched etc. until bar found or top reached AttributeError raised if not found Same thing with fields (foo.x)

Inheritance (4) Constructors: Calling init method on subclass doesn't automatically call superclass constructor! Can call superclass constructor explicitly if necessary

Inheritance (5) class Super: def init (self, x): self.x = x class Sub(Super): def init (self, y): Super. init (self, y) self.y = y

Inheritance example (1) class Animal: def init (self, weight): self.weight = weight def eat(self): print "I am eating!" def str (self): return "Animal; weight = %d" % \ self.weight

Inheritance example (2) >>> a = Animal(100) >>> a.eat() I am eating! >>> a.weight 100 >>> a.fly() AttributeError: Animal instance has no attribute 'fly'

Inheritance example (3) class Bird(Animal): def fly(self): print "I am flying!" b = Bird(100) # Animal's init () method b.eat() I am eating! b.fly() I am flying!

Exceptions again Can use inheritance to make it easy to generate simple exception subclasses: class MyException(Exception): pass This is the same as the previous MyException code, but much simpler to write Superclass (Exception) already does everything that MyException can do, so just inherit that functionality

Multiple inheritance (1) Multiple inheritance: class SubClass(Super1, Super2, Super3): <statement-1>.. <statement-n> Resolution rule for repeated attributes: Left-to-right, depth first search sorta... Actual rules are slightly more complex Don't depend on this if at all possible!

Multiple inheritance (2) Detailed rules: http://www.python.org/2.3/mro.html Usually used with "mixin" classes Combining two completely independent classes Ideally no fields or methods shared Conflicts then do not arise

Mixin example class DNASequence: # init etc. def getbasecounts(self):... # other DNA-specific methods class DBStorable: # init etc. # methods for storing into database class StorableDNASequence(DNASequence, \ DBStorable): # Override methods as needed # No common fields/methods in superclasses

Private fields Private fields of objects at least two leading underscores at most one trailing underscore e.g. spam spam à _<classname> spam <classname> is current class name Weak form of privacy protection

Next week We'll talk about the new features and changes in Python 3.x (3.0, 3.1, etc.)