Classes and Objects. Inheritance

Size: px
Start display at page:

Download "Classes and Objects. Inheritance"

Transcription

1 Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information.

2 Interpolating time series signals strength time

3 Interpolating time series signals strength time linear interpolation step function

4 Interpolating time series signals strength time linear interpolation A lot of the implementation is the same step function

5 Interpolating time series signals strength time linear interpolation A lot of the implementation is the same How can we eliminate step function the reduncancy?

6 First implementation class StepSignal(object):... def get(self, where): if where < self.values[0][0]: raise IndexError, '%f too low' % where for i in range(len(self.values)-1): x0, y0 = self.values[i] x1, y1 = self.values[i+1] if x0 <= where <= x1: return y0 raise IndexError, '%f too high' % where

7 Second implementation class LinearSignal(object):... def get(self, where): if where < self.values[0][0]: raise IndexError, '%f too low' % where for i in range(len(self.values)-1): x0, y0 = self.values[i] x1, y1 = self.values[i+1] if x0 <= where <= x1: return y0 + (y1-y0) * (where-x0) / (x1-x0) raise IndexError, '%f too high' % where

8 Second implementation class LinearSignal(object):... def get(self, where): if where < self.values[0][0]: raise IndexError, '%f too low' % where for i in range(len(self.values)-1): x0, y0 = self.values[i] x1, y1 = self.values[i+1] if x0 <= where <= x1: return y0 + (y1-y0) * (where-x0) / (x1-x0) raise IndexError, '%f too high' % where

9 Refactor class StepSignal(object): def get(self, where): i = self.find(self, where) return self.values[i][1] class LinearSignal(object): def get(self, where): i = self.find(self, where) x0, y0 = self.values[i] x1, y1 = self.values[i+1] return y0 + (y1-y0) * (where-x0)/(x1-x0)

10 Refactor class StepSignal(object): def get(self, where): i = self.find(self, where) return self.values[i][1] Where to put find? class LinearSignal(object): def get(self, where): i = self.find(self, where) x0, y0 = self.values[i] x1, y1 = self.values[i+1] return y0 + (y1-y0) * (where-x0)/(x1-x0)

11 Refactor class StepSignal(object): def get(self, where): i = self.find(self, where) return self.values[i][1] class LinearSignal(object): def get(self, where): Where to put find? Don't want to duplicate i = self.find(self, where) x0, y0 = self.values[i] x1, y1 = self.values[i+1] return y0 + (y1-y0) * (where-x0)/(x1-x0)

12 Use inheritance

13 Use inheritance class Parent(object): def hello(self): print 'hello'

14 Use inheritance class Parent(object): def hello(self): print 'hello' class Child(Parent): def goodbye(self): print 'goodbye'

15 Use inheritance class Parent(object): def hello(self): print 'hello' class Child(Parent): def goodbye(self): print 'goodbye' Child inherits from Parent

16 Use inheritance class Parent(object): def hello(self): print 'hello' c = Child() c.goodbye() goodbye class Child(Parent): def goodbye(self): print 'goodbye' Child inherits from Parent

17 Use inheritance class Parent(object): def hello(self): print 'hello' class Child(Parent): def goodbye(self): print 'goodbye' c = Child() c.goodbye() goodbye c.hello() hello Child inherits from Parent

18 Use inheritance class Parent(object): def hello(self): print 'hello' class Child(Parent): def goodbye(self): print 'goodbye' Child inherits c = Child() c.goodbye() goodbye c.hello() hello p = Parent() p.hello() hello from Parent

19 Use inheritance class Parent(object): def hello(self): print 'hello' class Child(Parent): def goodbye(self): print 'goodbye' Child inherits from Parent c = Child() c.goodbye() goodbye c.hello() hello p = Parent() p.hello() hello p.goodbye() AttributeError: 'Parent' object has no attribute 'goodbye'

20 Contents of memory object Parent hello Child goodbye c p

21 Contents of memory object Parent hello Child goodbye c p

22 Contents of memory object Parent hello Child goodbye c p

23 Contents of memory object Parent hello Child goodbye c p

24 Contents of memory object Parent hello Child goodbye c p

25 Contents of memory object Parent hello Child goodbye c p

26 class InterpolatedSignal(object): def find(self, where): if where < self.values[0][0]: raise IndexError, '%f too low' % where for i in range(len(self.values)-1): x0, y0 = self.values[i] x1, y1 = self.values[i+1] if x0 <= where <= x1: return i raise IndexError, '%f too high' % where

27 class InterpolatedSignal(object): def find(self, where): if where < self.values[0][0]: raise IndexError, '%f too low' % where for i in range(len(self.values)-1): x0, y0 = self.values[i] x1, y1 = self.values[i+1] if x0 <= where <= x1: return i raise IndexError, '%f too high' % where Not much use on its own

28 class InterpolatedSignal(object): def find(self, where): if where < self.values[0][0]: raise IndexError, '%f too low' % where for i in range(len(self.values)-1): x0, y0 = self.values[i] x1, y1 = self.values[i+1] if x0 <= where <= x1: return i raise IndexError, '%f too high' % where Not much use on its own Where does this come from?

29 Derive specific interpolators

30 Derive specific interpolators class StepSignal(InterpolatedSignal): def init (self, values): self.values = values[:] def get(self, where): i = self.find(where) return self.values[i][1]

31 Derive specific interpolators class StepSignal(InterpolatedSignal): def init (self, values): self.values = values[:] def get(self, where): i = self.find(where) return self.values[i][1]

32 Derive specific interpolators class StepSignal(InterpolatedSignal): def init (self, values): self.values = values[:] def get(self, where): i = self.find(where) return self.values[i][1]

33 Derive specific interpolators class StepSignal(InterpolatedSignal): def init (self, values): self.values = values[:] def get(self, where): i = self.find(where) return self.values[i][1]

34 Derive specific interpolators class StepSignal(InterpolatedSignal): def init (self, values): self.values = values[:] def get(self, where): i = self.find(where) return self.values[i][1] Fragile

35 Derive specific interpolators class StepSignal(InterpolatedSignal): def init (self, values): self.values = values[:] def get(self, where): i = self.find(where) return self.values[i][1] Dependencies between classes should be explicit

36 Have the parent class store the values

37 Have the parent class store the values class InterpolatedSignal(object): def init (self, values): self.values = values[:] def get(self, where): raise NotImplementedError('Must provide get!') def find(self, where):...as before...

38 Have the parent class store the values class InterpolatedSignal(object): def init (self, values): self.values = values[:] def get(self, where): raise NotImplementedError('Must provide get!') def find(self, where):...as before...

39 The child's constructor relies on the parent's class StepSignal(InterpolatedSignal): def init (self, values): InterpolatedSignal. init (self, values) def get(self, where): i = self.find(where) return self.values[i][1]

40 The child's constructor relies on the parent's class StepSignal(InterpolatedSignal): def init (self, values): InterpolatedSignal. init (self, values) def get(self, where): i = self.find(where) return self.values[i][1]

41 Other classes are just as easy Class LinearSignal(InterpolatedSignal): def init (self, values): InterpolatedSignal. init (self, values) def get(self, where): i = self.find(where) return y0 + (y1-y0) * (where-x0)/(x1-x0)

42 class InterpolatedSignal(object): def init (self, values): assert len(values) > 0, 'Must have some pairs' for i in range(len(values)): assert len(values[i]) == 2, 'Entries must be pairs' for i in range(len(values)-1)): x0 = values[i][0] x1 = values[i][1] assert x0 < x1, 'Samples must increase on x' >>> signal = StepSignal([ [1., 0.], [0., 2] ]) AssertionError: Samples must increase on x

43 Child overrides parent method object InterpolatedSignal init find get StepSignal init get signal

44 Overriding in action

45 Overriding in action class Parent(object): def hello(self): print 'hello' def goodbye(self): print 'goodbye'

46 Overriding in action class Parent(object): def hello(self): print 'hello' def goodbye(self): print 'goodbye' class Child(Parent): def goodbye(self): print 'au revoir'

47 Overriding in action class Parent(object): def hello(self): print 'hello' def goodbye(self): print 'goodbye' class Child(Parent): def goodbye(self): print 'au revoir' Child overrides Parent

48 Overriding in action class Parent(object): def hello(self): print 'hello' def goodbye(self): print 'goodbye' p = Parent() p.hello() hello p.goodbye() goodbye class Child(Parent): def goodbye(self): print 'au revoir'

49 Overriding in action class Parent(object): def hello(self): print 'hello' def goodbye(self): print 'goodbye' class Child(Parent): def goodbye(self): print 'au revoir' p = Parent() p.hello() hello p.goodbye() goodbye C = child() c.hello() hello

50 Overriding in action class Parent(object): def hello(self): print 'hello' def goodbye(self): print 'goodbye' class Child(Parent): def goodbye(self): print 'au revoir' p = Parent() p.hello() hello p.goodbye() goodbye C = child() c.hello() hello c.goodbye() au revoir

51 created by Greg Wilson January 2011 Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information.

A list is a mutable heterogeneous sequence

A list is a mutable heterogeneous sequence Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. A list is a mutable heterogeneous

More information

Scientific Computing (Phys 2109/ Ast 3100H) I. Scientfic Software Development. SciNet HPC Consortium

Scientific Computing (Phys 2109/ Ast 3100H) I. Scientfic Software Development. SciNet HPC Consortium Scientific Computing (Phys 2109/ Ast 3100H) I. Scientfic Software Development SciNet HPC Consortium HW3 Issues How to do the timesteping (in what sense does the coupling operate?) Tracer Theory doesn t

More information

CS 11 python track: lecture 4

CS 11 python track: lecture 4 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

More information

Automated Builds. Macros

Automated Builds. Macros Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. Manage tasks and dependencies

More information

The Unix Shell. Job Control

The Unix Shell. Job Control The Unix Shell Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. shell shell $

More information

Automated Builds. Rules

Automated Builds. Rules Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. Manage tasks and dependencies

More information

Review 3. Exceptions and Try-Except Blocks

Review 3. Exceptions and Try-Except Blocks Review 3 Exceptions and Try-Except Blocks What Might You Be Asked Create your own Exception class Write code to throw an exception Follow the path of a thrown exception Requires understanding of try-except

More information

The Unix Shell. Pipes and Filters

The Unix Shell. Pipes and Filters The Unix Shell Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. shell shell pwd

More information

Effective Programming Practices for Economists

Effective Programming Practices for Economists Effective Programming Practices for Economists 14. Object-oriented programming Hans-Martin von Gaudecker Department of Economics, Universität Bonn Revisiting abstractions Reminder of the example Reconsider

More information

CS 11 python track: lecture 2

CS 11 python track: lecture 2 CS 11 python track: lecture 2 Today: Odds and ends Introduction to object-oriented programming Exception handling Odds and ends List slice notation Multiline strings Docstrings List slices (1) a = [1,

More information

Exceptions CS GMU

Exceptions CS GMU Exceptions CS 112 @ GMU Exceptions When an unrecoverable action takes place, normal control flow is abandoned: an exception value crashes outwards until caught. various types of exception values can be

More information

Abstract Data Types Chapter 1

Abstract Data Types Chapter 1 Abstract Data Types Chapter 1 Part Two Bags A bag is a basic container like a shopping bag that can be used to store collections. There are several variations: simple bag grab bag counting bag 2 Bag ADT

More information

n 1 i = n + i = n + f(n 1)

n 1 i = n + i = n + f(n 1) 2 Binary Search Trees Lab Objective: A tree is a linked list where each node in the list may refer to more than one other node. This structural flexibility makes trees more useful and efficient than regular

More information

Python. First-Class Functions

Python. First-Class Functions Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. An integer is 32 bits of data...

More information

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures Outline Chapter 7: Trees 1 Chapter 7: Trees Approaching BST Making a decision We discussed the trade-offs between linked and array-based implementations of sequences (back in Section 4.7). Linked lists

More information

A programming language should not include everything anyone might ever want

A programming language should not include everything anyone might ever want Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. A programming language should

More information

Example: Class MSDie Introduction to Graphs and NetworkX

Example: Class MSDie Introduction to Graphs and NetworkX Example: Class MSDie Introduction to Graphs and NetworkX Monday, March 30, 2009 1 Be careful whose random number generator you trust http://xkcd.com/221/ 2 1 Reminders Sample exam questions have been posted

More information

Python Essential Reference, Second Edition - Chapter 5: Control Flow Page 1 of 8

Python Essential Reference, Second Edition - Chapter 5: Control Flow Page 1 of 8 Python Essential Reference, Second Edition - Chapter 5: Control Flow Page 1 of 8 Chapter 5: Control Flow This chapter describes related to the control flow of a program. Topics include conditionals, loops,

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College October 24, 2016 Outline Outline 1 Chapter 7: Trees Outline Chapter 7: Trees 1 Chapter 7: Trees The Binary Search Property

More information

CSE : Python Programming

CSE : Python Programming CSE 399-004: Python Programming Lecture 2: Data, Classes, and Modules January 22, 2007 http://www.seas.upenn.edu/~cse39904/ Administrative things Teaching assistant Brian Summa (bsumma @ seas.upenn.edu)

More information

Multimedia Programming

Multimedia Programming Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. Data is 1's and 0's Data is 1's

More information

Week 8 Lecture: Programming

Week 8 Lecture: Programming Week 8 Lecture: Introduction to Object Oriented Programming Introduction to Programming for GIS & Remote Sensing GEO6938 1469 GEO4938 147A Objects: You ve Been Exposed! Modularization, you know about it

More information

FILE HANDLING AND EXCEPTIONS

FILE HANDLING AND EXCEPTIONS FILE HANDLING AND EXCEPTIONS INPUT We ve already seen how to use the input function for grabbing input from a user: input() >>> print(input('what is your name? ')) What is your name? Spongebob Spongebob

More information

Fun with Python Descriptors

Fun with Python Descriptors Fun with Python Descriptors Author: Jeff Rush Date: 2006-11-25 include directive disabled... include:: Overview What are descriptors? Implementation: attribute watcher Implementation: property()

More information

Object-oriented programming in Python (2)

Object-oriented programming in Python (2) Programming Languages Week 9 Object-oriented programming in Python (2) College of Information Science and Engineering Ritsumeikan University plan last week: dictionaries vs. objects classes, instances,

More information

Sets and Dictionaries

Sets and Dictionaries Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. Let's try an experiment Let's

More information

Lecture #12: Quick: Exceptions and SQL

Lecture #12: Quick: Exceptions and SQL UC Berkeley EECS Adj. Assistant Prof. Dr. Gerald Friedland Computational Structures in Data Science Lecture #12: Quick: Exceptions and SQL Administrivia Open Project: Starts Monday! Creative data task

More information

Object-Oriented Python

Object-Oriented Python 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

More information

Software Carpentry. Nicola Chiapolini. Physik-Institut University of Zurich. June 16, 2015

Software Carpentry. Nicola Chiapolini. Physik-Institut University of Zurich. June 16, 2015 Nicola Chiapolini, June 16, 2015 1 / 45 Software Carpentry Nicola Chiapolini Physik-Institut University of Zurich June 16, 2015 Based on a talk by Pietro Berkes This work is licensed under the Creative

More information

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

Python OOP. Stéphane Vialette. LIGM, Université Paris-Est Marne-la-Vallée. October 19, 2010 Python OOP Stéphane Vialette LIGM, Université Paris-Est Marne-la-Vallée October 19, 2010 Stéphane Vialette (LIGM, Université Paris-Est Marne-la-Vallée) Python OOP October 19, 2010 1 / 59 Outline 1 Introduction

More information

A simple interpreted language

A simple interpreted language Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. A simple interpreted language

More information

(2½ hours) Total Marks: 75

(2½ hours) Total Marks: 75 (2½ hours) Total Marks: 75 N. B.: (1) All questions are compulsory. (2) Make suitable assumptions wherever necessary and state the assumptions made. (3) Answers to the same question must be written together.

More information

pytest-factoryboy Documentation

pytest-factoryboy Documentation pytest-factoryboy Documentation Release 2.0.1 Oleg Pidsadnyi, Anatoly Bubenkov and others Jun 30, 2018 Contents 1 factory_boy integration with the pytest runner 3 1.1 Install pytest-factoryboy.........................................

More information

Multithreading. processes and threads life cycle of a thread. the thread module the Thread class. two different algorithms run concurrently

Multithreading. processes and threads life cycle of a thread. the thread module the Thread class. two different algorithms run concurrently Multithreading 1 Concurrent Processes processes and threads life cycle of a thread 2 Multithreading in Python the thread module the Thread class 3 Producer/Consumer Relation two different algorithms run

More information

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

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples Abstract Data Types CS 234, Fall 2017 Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples Data Types Data is stored in a computer as a sequence of binary digits:

More information

public Twix() { calories = 285; ingredients = "chocolate, sugar, cookie, caramel"; }

public Twix() { calories = 285; ingredients = chocolate, sugar, cookie, caramel; } Additional inheritance example As another example of inheritance, perhaps we would like to build an application about candy. For starters, let s say we want to do something with Twix bars and something

More information

Design Pattern: Composite

Design Pattern: Composite Design Pattern: Composite Intent Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. Motivation

More information

Object Model Comparisons

Object Model Comparisons Object Model Comparisons 1 Languages are designed, just like programs Someone decides what the language is for Someone decides what features it's going to have Can't really understand a language until

More information

CSC148 Intro. to Computer Science

CSC148 Intro. to Computer Science CSC148 Intro. to Computer Science Lecture 4: Container implementation, Unit Test, Balanced Parentheses, Intro to Linked Lists Amir H. Chinaei, Summer 2016 Office Hours: R 10-12 BA4222 ahchinaei@cs.toronto.edu

More information

Version Control. Ioannis N. Athanasiadis. with slides from Solution Perspective Media and Software Carpentry

Version Control. Ioannis N. Athanasiadis. with slides from Solution Perspective Media and Software Carpentry Ioannis N. Athanasiadis with slides from Solution Perspective Media and Software Carpentry http://springuniversity.bc3research.org/ 1 What is it A method for centrally storing files Keeping a record of

More information

Exceptions & a Taste of Declarative Programming in SQL

Exceptions & a Taste of Declarative Programming in SQL Exceptions & a Taste of Declarative Programming in SQL David E. Culler CS8 Computational Structures in Data Science http://inst.eecs.berkeley.edu/~cs88 Lecture 12 April 18, 2016 Computational Concepts

More information

CS61A Lecture 32. Amir Kamil UC Berkeley April 5, 2013

CS61A Lecture 32. Amir Kamil UC Berkeley April 5, 2013 CS61A Lecture 32 Amir Kamil UC Berkeley April 5, 2013 Announcements Hog revisions due Monday HW10 due Wednesday Make sure to fill out survey on Piazza We need to schedule alternate final exam times for

More information

Data Structures I: Linked Lists

Data Structures I: Linked Lists Lab 4 Data Structures I: Linked Lists Lab Objective: Analyzing and manipulating data are essential skills in scientific computing. Storing, retrieving, and rearranging data take time. As a dataset grows,

More information

Python. Directory and File Paths

Python. Directory and File Paths Copyright Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.

More information

A function is a way to turn a bunch of related statements into a single "chunk"

A function is a way to turn a bunch of related statements into a single chunk Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. A function is a way to turn a

More information

The Unix Shell. Job Control

The Unix Shell. Job Control The Unix Shell Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. shell shell $

More information

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

Exceptions. raise type(message) raise Exception(message) Classes Special thanks to Roy McElmurry, Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/.0

More information

Lecture 21. Programming with Subclasses

Lecture 21. Programming with Subclasses Lecture 21 Programming with Subclasses Announcements for This Lecture Assignments Prelim 2 A4 is now graded Mean: 90.4 Median: 93 Std Dev: 10.6 Mean: 9 hrs Median: 8 hrs Std Dev: 4.1 hrs A5 is also graded

More information

LECTURE 4 Python Basics Part 3

LECTURE 4 Python Basics Part 3 LECTURE 4 Python Basics Part 3 INPUT We ve already seen two useful functions for grabbing input from a user: raw_input() Asks the user for a string of input, and returns the string. If you provide an argument,

More information

The Unix Shell. Permissions

The Unix Shell. Permissions The Unix Shell Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. shell shell pwd,

More information

Inheritance. = way of forming new classes based on existing ones = way to share/reuse codebetween two or more classes

Inheritance. = way of forming new classes based on existing ones = way to share/reuse codebetween two or more classes Inheritance 49 Inheritance Definitions = way of forming new classes based on existing ones = way to share/reuse codebetween two or more classes Terminology superclass: Parent class being inherited from

More information

Python. Input and Output

Python. Input and Output Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. Been using print to see what

More information

COMP1730/COMP6730 Programming for Scientists. Exceptions and exception handling

COMP1730/COMP6730 Programming for Scientists. Exceptions and exception handling COMP1730/COMP6730 Programming for Scientists Exceptions and exception handling Lecture outline * Errors * The exception mechanism in python * Causing exceptions (assert and raise) * Handling exceptions

More information

Lecture 21. Programming with Subclasses

Lecture 21. Programming with Subclasses Lecture 21 Programming with Subclasses Announcements for Today Reading Today: See reading online Tuesday: Chapter 7 Prelim, Nov 10 th 7:30-9:00 Material up to Today Review has been posted Recursion + Loops

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism The concept of inheritance is a common feature of an object-oriented programming language. Inheritance allows a programmer to define a general class, and then later define

More information

CSC148-Section:L0301

CSC148-Section:L0301 CSC148-Section:L0301 Week#2-Wednesday Instructed by AbdulAziz Al-Helali a.alhelali@mail.utoronto.ca Office hours: Wednesday 11-1, BA2230. Slides adapted from Professor Danny Heap and Jacqueline Smith slides

More information

Lecture 21. Programming with Subclasses

Lecture 21. Programming with Subclasses Lecture 21 Programming with Subclasses Announcements for Today Reading Today: See reading online Tuesday: Chapter 7 Prelim, Nov 9 th 7:30-9:00 Material up to Today Review has been posted Recursion + Loops

More information

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance (extends) Use relationship Inheritance 1..n Unidirectional association Inheritance ("extends") Use relationship Implementation ("implements") What is inherited public, protected methods public, proteced attributes What is not inherited

More information

CS Programming I: Inheritance

CS Programming I: Inheritance CS 200 - Programming I: Inheritance Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Inheritance

More information

PIC 16: Midterm. Part 1. In this part, you should not worry about why your class has the name that it does.

PIC 16: Midterm. Part 1. In this part, you should not worry about why your class has the name that it does. PIC 16: Midterm Assigned 10/19/2018 at noon. Code (a single.py file) due by 12:51pm on CCLE. I have provided you with a.py file which contains test code. I have also provided you with a.txt file of what

More information

Lecture 18. Classes and Types

Lecture 18. Classes and Types Lecture 18 Classes and Types Announcements for Today Reading Today: See reading online Tuesday: See reading online Prelim, Nov 6 th 7:30-9:30 Material up to next class Review posted next week Recursion

More information

Inheritance and Encapsulation. Amit Gupta

Inheritance and Encapsulation. Amit Gupta Inheritance and Encapsulation Amit Gupta Project 1 How did it go? What did you like about it? What did you not like? What can we do to help? Suggestions Ask questions if you don t understand a concept

More information

Programming overview

Programming overview Programming overview Basic Java A Java program consists of: One or more classes A class contains one or more methods A method contains program statements Each class in a separate file MyClass defined in

More information

The Unix Shell. Files and Directories

The Unix Shell. Files and Directories The Unix Shell Copyright Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. Run Programs Store

More information

Polymorphism: Inheritance Interfaces

Polymorphism: Inheritance Interfaces Polymorphism: Inheritance Interfaces 256 Recap Finish Deck class methods Questions about assignments? Review Player class for HW9 (starter zip posted) Lessons Learned: Arrays of objects require multiple

More information

CS 1110 Final, December 8th, Question Points Score Total: 100

CS 1110 Final, December 8th, Question Points Score Total: 100 CS 1110 Final, December 8th, 2016 This 150-minute exam has 8 questions worth a total of 100 points. Scan the whole test before starting. Budget your time wisely. Use the back of the pages if you need more

More information

Implementing AddChoice()

Implementing AddChoice() Implementing AddChoice() The method will receive three parameters As usual for a class method the self parameter is required The text for the choice A Boolean deno9ng if it is the correct choice or not

More information

Mobile Application Programming. Swift Classes

Mobile Application Programming. Swift Classes Mobile Application Programming Swift Classes Swift Objects Classes, structures, and enums are all object types with different defaults in usage Classes are reference types that share the same object when

More information

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

Data Structures (list, dictionary, tuples, sets, strings) Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in brackets: l = [1, 2, "a"] (access by index, is mutable sequence) Tuples are enclosed in parentheses: t = (1, 2, "a") (access

More information

61A Lecture 25. Friday, October 28

61A Lecture 25. Friday, October 28 61A Lecture 25 Friday, October 2 From Last Time: Adjoining to a Tree Set 5 9 7 3 9 7 11 1 7 11 Right! Left! Right! Stop! 5 9 7 3 9 7 11 1 7 11 2 From the Exam: Pruned Trees a b c d (a,b) (a,c) (a,d) pruned

More information

INHERITANCE AND EXTENDING CLASSES

INHERITANCE AND EXTENDING CLASSES INHERITANCE AND EXTENDING CLASSES Java programmers often take advantage of a feature of object-oriented programming called inheritance, which allows programmers to make one class an extension of another

More information

CS Prelim 2 Review Fall 2015

CS Prelim 2 Review Fall 2015 CS 1110 Prelim 2 Review Fall 2015 Exam Info Prelim 2: 7:30 9:00PM, Thursday, Nov. 12th Last name A J in Uris G01 Last name K Z in Statler Auditorium SDS Students will get an e-mail To help you study: Study

More information

CS Prelim 2 Review Fall 2014

CS Prelim 2 Review Fall 2014 CS 1110 Prelim 2 Review Fall 2014 Exam Info Prelim 2: 7:30 9:00PM, Thursday, Nov. 13th Last name A Sh in Statler Auditorium Last name Si X in Statler 196 Last name Y Z in Statler 198 SDS Students will

More information

Scientific Computing with Python and CUDA

Scientific Computing with Python and CUDA Scientific Computing with Python and CUDA Stefan Reiterer High Performance Computing Seminar, January 17 2011 Stefan Reiterer () Scientific Computing with Python and CUDA HPC Seminar 1 / 55 Inhalt 1 A

More information

PREPARING FOR THE FINAL EXAM

PREPARING FOR THE FINAL EXAM PREPARING FOR THE FINAL EXAM CS 1110: FALL 2017 This handout explains what you have to know for the final exam. Most of the exam will include topics from the previous two prelims. We have uploaded the

More information

COMP 110/L Lecture 19. Kyle Dewey

COMP 110/L Lecture 19. Kyle Dewey COMP 110/L Lecture 19 Kyle Dewey Outline Inheritance extends super Method overriding Automatically-generated constructors Inheritance Recap -We talked about object-oriented programming being about objects

More information

Search. The Nearest Neighbor Problem

Search. The Nearest Neighbor Problem 3 Nearest Neighbor Search Lab Objective: The nearest neighbor problem is an optimization problem that arises in applications such as computer vision, pattern recognition, internet marketing, and data compression.

More information

Exceptions and File I/O

Exceptions and File I/O Lab 6 Exceptions and File I/O Lab Objective: In Python, an exception is an error detected during execution. Exceptions are important for regulating program usage and for correctly reporting problems to

More information

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

Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors Objects (again) Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction to Computer Science, 2 nd edition, by John

More information

03 Pyro. Python Remote Objects. Distributed Object Technology. 100% pure Python Naming Service Mobile objects Exceptions transports

03 Pyro. Python Remote Objects. Distributed Object Technology. 100% pure Python Naming Service Mobile objects Exceptions transports 03 Pyro Python Remote Objects Pyro3 - https://pythonhosted.org/pyro/ Pyro4 - https://pythonhosted.org/pyro4/ Distributed Object Technology RMI Mobile code 100% pure Python Naming Service Mobile objects

More information

List of lectures. Lecture content. Lecture goal. Object Oriented Programming. Object-oriented and type. TDDA69 Data and Program Structure

List of lectures. Lecture content. Lecture goal. Object Oriented Programming. Object-oriented and type. TDDA69 Data and Program Structure List of lectures TDDA69 Data and Program Structure Object Oriented Programming and type system Cyrille Berger 1 Introduction and Functional Programming 2 Imperative Programming and Data Structures 3 Environment

More information

Defining Classes. CS Recitation 11

Defining Classes. CS Recitation 11 Defining Classes CS 177 - Recitation 11 Recap Previous week We know how to define functions Define a function: Call a function: def function(value): temp = value * value return temp def main(): result

More information

MITOCW watch?v=flgjisf3l78

MITOCW watch?v=flgjisf3l78 MITOCW watch?v=flgjisf3l78 The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high-quality educational resources for free. To

More information

CSC207 Week 3. Larry Zhang

CSC207 Week 3. Larry Zhang CSC207 Week 3 Larry Zhang 1 Announcements Readings will be posted before the lecture Lab 1 marks available in your repo 1 point for creating the correct project. 1 point for creating the correct classes.

More information

CSC148: Week 2

CSC148: Week 2 CSC148: Week 2 http://www.cdf.utoronto.ca/~csc148h/summer/ Sophia Huynh Summer 2018 1 Annoucements Ex1 due tonight @ 11PM Ex2 will be released tonight No lab next week "Lab 2" will be released tonight

More information

Implementing Interfaces. Marwan Burelle. July 20, 2012

Implementing Interfaces. Marwan Burelle. July 20, 2012 Implementing marwan.burelle@lse.epita.fr http://www.lse.epita.fr/ July 20, 2012 Outline 1 2 3 4 Quick Overview of System oriented programming language Variant of C with a rationnalized syntax. Syntactic

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

Programming I. Course 9 Introduction to programming

Programming I. Course 9 Introduction to programming Programming I Course 9 Introduction to programming What we talked about? Modules List Comprehension Generators Recursive Functions Files What we talk today? Object Oriented Programming Classes Objects

More information

Mobile Application Programming. Swift Classes

Mobile Application Programming. Swift Classes Mobile Application Programming Swift Classes Swift Top-Level Entities Like C/C++ but unlike Java, Swift allows declarations of functions, variables, and constants at the top-level, outside any class declaration

More information

C++ Important Questions with Answers

C++ Important Questions with Answers 1. Name the operators that cannot be overloaded. sizeof,.,.*,.->, ::,? 2. What is inheritance? Inheritance is property such that a parent (or super) class passes the characteristics of itself to children

More information

Test-Driven Development JUnit

Test-Driven Development JUnit Test-Driven Development JUnit Click to edit Master EECS text 2311 styles - Software Development Project Second level Third level Fourth level Fifth level Wednesday, January 18, 2017 1 Simulator submission

More information

Pytest C. Release. John McNamara

Pytest C. Release. John McNamara Pytest C Release John McNamara Mar 08, 2018 Contents 1 The C Unit Tests 1 2 The Pytest hooks 3 2.1 The conftest.py file............................................ 3 2.2 Finding test files.............................................

More information

1 A node in a tree. Trees. Datatypes ISC

1 A node in a tree. Trees. Datatypes ISC Datatypes ISC-5315 1 1 A node in a tree /*C*/ typedef struct _node { struct _node *left; struct _node *right; struct _node *parent; float value; node; # # Python example class Node: Node class: this is

More information

Review 2. Classes and Subclasses

Review 2. Classes and Subclasses Review 2 Classes and Subclasses Class Definition class (): """Class specification""" class variables (format: Class.variable) initializer ( init ) special method definitions

More information

Groovy. Extending Java with scripting capabilities. Last updated: 10 July 2017

Groovy. Extending Java with scripting capabilities. Last updated: 10 July 2017 Groovy Extending Java with scripting capabilities Last updated: 10 July 2017 Pepgo Limited, 71-75 Shelton Street, Covent Garden, London, WC2H 9JQ, United Kingdom Contents About Groovy... 3 Install Groovy...

More information

Seshat Documentation. Release Joshua P Ashby

Seshat Documentation. Release Joshua P Ashby Seshat Documentation Release 1.0.0 Joshua P Ashby Apr 05, 2017 Contents 1 A Few Minor Warnings 3 2 Quick Start 5 2.1 Contributing............................................... 5 2.2 Doc Contents...............................................

More information

Ch.9: Object-oriented programming

Ch.9: Object-oriented programming Ch.9: Object-oriented programming Hans Petter Langtangen 1,2 Simula Research Laboratory 1 University of Oslo, Dept. of Informatics 2 Aug 15, 2015 1 Inheritance Inheritance The chapter title Object-oriented

More information

Mastering Python Decorators

Mastering Python Decorators Mastering Python Decorators One of the hallmarks of good Python is the judicious use of decorators to optimize, simplify and add new functionality to existing code. Decorators are usually seen as an advanced

More information

Inheritance. For example, to zoom in on the deer family (cervid), we could make a tree like the following.

Inheritance. For example, to zoom in on the deer family (cervid), we could make a tree like the following. Inheritance The concept of inheritance is one of the key features of an object-oriented programming language. Inheritance allows a programmer to define a general class, and then later define more specific

More information

Babu Madhav Institute of Information Technology, UTU 2015

Babu Madhav Institute of Information Technology, UTU 2015 Five years Integrated M.Sc.(IT)(Semester 5) Question Bank 060010502:Programming in Python Unit-1:Introduction To Python Q-1 Answer the following Questions in short. 1. Which operator is used for slicing?

More information