Classes, part Deux. Three Groups. CSE 231, Rich Enbody. Users Programmers Class Designers 11/11/13. Michigan State University CSE 231, Fall 2013

Size: px
Start display at page:

Download "Classes, part Deux. Three Groups. CSE 231, Rich Enbody. Users Programmers Class Designers 11/11/13. Michigan State University CSE 231, Fall 2013"

Transcription

1 , part Deux CSE 231, Rich Enbody Three Groups Users Programmers Class Designers 1

2 Getters and Setters getters: methods that fetch the value setters: methods set a value 2

3 GetterSetter Example Why do this? OOP: controlling access to an instance as the designer intended. 3

4 Why not to do this? Have to create all those functions. Pythonistas want to do dot access, (easier to read). Compromise: propname = property(fget=getfn, fset=setfn, fdel=delfn) propname is now a variable in the instance with three associated functions. 4

5 Read Only: for any property, you can fail to set the fset portion, meaning it becomes read only. (You could also make it write only, though that would be kind of weird.) Circle Example 5

6 Setup We create three properties: radius, area and circumference We also create three hidden variables to hold values: radius, area and circumference Access goes through the 3 properties, backed by the three hidden variables. Radius Only the radius property can be changed. Updating radius updates the other two. You can t change area and circumference as they are readonly 6

7 Caution! You cannot do the following def get_radius(self):! return self.radius # no! radius = property(fget=get_radius)! Infinite regress! Property is the external interface, vars are the internal interface. Result Programmer can use standard interface (dot reference) to variables. Designer can place restrictions on access to variables. Designer can refactor the code later without changing the interface. More readable code. 7

8 Remember, we said there were three factors that distinguished an Object Oriented Programming language: o encapsulation o inheritance o polymorphism We are still at encapsulation We said that encapsulation: hid details of the implementation so the program was easier to read and write provides modularity, so objects can be reused in other contexts provides an interface (the methods) that is the approved way to deal with the class 8

9 A new aspect we should have is consistency: a new class should respond to standard messages in a way that is consistent with how other data structures behaved. Consider a rational number class. It should respond to: o construction o printing o arithmetic ops (+, -, *, /) o comparison ops (<, >, <=, >=) 9

10 If we define the class in a consistent way, the following program should work: = (1,2) r2 = (3,4) # constructor # addition print(r3) # print if ( <= r2): # comparison r2 = r2 - # subtraction just like any other number By building the class in a consistent way, we can make a new instance of look like any other number syntactically. The instance responds to all the normal function calls. 10

11 How can that work? Python can distinguish which operator to use based on types. Python provides special function names that represent the action of standard operations in the language. A class is essentially a new type when we make an instance of a class, we have made an object of a particular type 1.36 is a float my_instance=myclass(), my_instance is a type of myclass 11

12 Python does not have a type associated with any variable, since each variable is allowed to reference any object. However, we can ask any variable what type it presently references. This is called introspection. Python introspection ops type(variable) returns its type (as an object) isinstance(variable,type) returns a Boolean indicating if the variable is of that type. 12

13 Introspection Example So what does va+var2 mean? it depends The + operation has two operands. What are their types? Python uses introspection to find the type and then select the correct operator. 13

14 What does va+var2 do? with two strings, we get concatenation with two integers, we get addition with an integer and a string we get: Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> 1+'a' TypeError: unsupported operand type(s) for +: 'int' and 'str' The plus operator is overloaded: the operation depends on the types involved 14

15 Some Python operators can be overloaded. o Like all the special class operations, the names use two underlines before and after the name o They come in two general classes: numeric type operations (+,-,<,>,print etc.) container operations ([ ], iterate, len, etc.) lets look at one first class MyClass(object): # other defs, including def (self,arg2): # do something v1 = MyClass() v2 = MyClass() v1 + v2 15

16 v1+v2 maps to v1 + v2 v1.(v2) def (self,arg2): v1 is bound to self, v2 bound to other parameter Number Class 16

17 Simple Number class A is represented by two numbers, the numerator and the denominator. We can apply many of the numeric operators to. Simple Number class A is represented by two numbers, the numerator and the denominator. class (object): """ number class""" def (self, numer, denom=1): """numer: numerator, denom: denominator "" self.numer = numer self.denom = denom def 17

18 Constructing an instance class (object): """ number class""" def (self,numer,denom=1): """numer: numerator, denom: denominator "" print("in constructor") self.numer = numer self.denom = denom def (self,f): s N.S. = (3,4) r2 = (1,2) Constructing an instance class (object): """ number class""" def (self,numer,denom=1): """numer: numerator, denom: denominator "" print("in constructor") self.numer = numer self.denom = denom def (self,f): s N.S. = (3,4) r2 = (1,2) s N.S. self numer denom 18

19 Constructing an instance class (object): """ number class""" def (self,numer,denom=1): """numer: numerator, denom: denominator "" print("in constructor") self.numer = numer self.denom = denom def (self,f): = (3,4) r2 = (1,2) s N.S. self s N.S. numer denom Constructing an instance class (object): in constructor! """ number class""" def (self,numer,denom=1): """numer: numerator, denom: denominator "" print("in constructor") self.numer = numer self.denom = denom def (self,f): s N.S. = (3,4) r2 = (1,2) s N.S. self numer denom 19

20 Constructing an instance class (object): in constructor! """ number class""" def (self,numer,denom=1): """numer: numerator, denom: denominator "" print("in constructor") self.numer = numer self.denom = denom def (self,f): s N.S. = (3,4) r2 = (1,2) s N.S. self Constructing an instance class (object): in constructor! """ number class""" def (self,numer,denom=1): """numer: numerator, denom: denominator "" print("in constructor") self.numer = numer self.denom = denom def (self,f): s N.S. = (3,4) r2 = (1,2) s N.S. self 20

21 Constructing an instance class (object): in constructor! """ number class""" def (self,numer,denom=1): """numer: numerator, denom: denominator "" print("in constructor") self.numer = numer self.denom = denom def (self,f): s N.S. = (3,4) r2 = (1,2) s N.S. self Constructing an instance class (object): in constructor! """ number class""" def (self,numer,denom=1): """numer: numerator, denom: denominator "" print("in constructor") self.numer = numer self.denom = denom def (self,f): s N.S. = (3,4) r2 = (1,2) 21

22 Constructing an instance class (object): in constructor! """ number class""" def (self,numer,denom=1): """numer: numerator, denom: denominator "" print("in constructor") self.numer = numer self.denom = denom def (self,f): s N.S. = (3,4) r2 = (1,2) s N.S. self numer denom Constructing an instance class (object): in constructor! """ number class""" def (self,numer,denom=1): """numer: numerator, denom: denominator "" print("in constructor") self.numer = numer self.denom = denom def (self,f): s N.S. = (3,4) r2 = (1,2) s N.S. self 22

23 Constructing an instance class (object): in constructor """ number class""" in constructor! def (self,numer,denom=1): """numer: numerator, denom: denominator "" print("in constructor") s N.S. self.numer = numer self.denom = denom def (self,f): = (3,4) r2 = (1,2) s N.S. self Constructing an instance class (object): in constructor """ number class""" in constructor! def (self,numer,denom=1): """numer: numerator, denom: denominator "" print("in constructor") s N.S. self.numer = numer self.denom = denom def (self,f): = (3,4) r2 = (1,2) r2 23

24 Addition of s class (object): in constructor """ number class""" in constructor! def (self,numer,denom=1): """numer: numerator, denom: denominator "" print("in constructor") s N.S. self.numer = numer self.denom = denom def (self,f): = (3,4) r2 = (1,2) r3 =.(r2) r2 Addition of s def (self, f): in constructor print('in add') in constructor! thelcm = lcm(self.denom,f.denom) thesum = (thelcm/self.denom*self.numer)+\ (thelcm/f.denom * f.numer) s N.S. return (thesum,thelcm) r2 = (3,4) r2 = (1,2) r3 =.(r2) s N.S. self f 24

25 Addition of s def (self, f): in constructor print('in add') in constructor! thelcm = lcm(self.denom,f.denom) thesum = (thelcm/self.denom*self.numer)+\ (thelcm/f.denom * f.numer) s N.S. return (thesum,thelcm) r2 = (3,4) r2 = (1,2) r3 =.(r2) s N.S. self f Addition of s in constructor def (self, f): in constructor print('in add') in add! thelcm = lcm(self.denom,f.denom) thesum = (thelcm/self.denom*self.numer)+\ (thelcm/f.denom * f.numer) s N.S. return (thesum,thelcm) r2 = (3,4) r2 = (1,2) r3 =.(r2) s N.S. self f thelcm thesum 25

26 Addition of s in constructor in constructor def (self, f): in constructor add print('in add') in constructor! thelcm = lcm(self.denom,f.denom) thesum = (thelcm/self.denom*self.numer)+\ (thelcm/f.denom * f.numer) return (thesum,thelcm) = (3,4) r2 = (1,2) r2 r3 numer 5 denom 4 Operators (self,f2) sub (self,f2) (self,num,denom=1) str (self) repr (self) eq (self,f2) 26

27 Subtraction of s def sub (self, f): """return the difference """ thelcm = lcm(self.denom,f.denom) thediff = (thelcm/self.denom * self.numer) - \ (thelcm/f.denom * f.numer) return (thediff,thelcm) = (3,4) r2 = (1,2) r2 = r2 r2 r3 numer 5 denom 4 Subtraction of s def sub (self, f): """return the difference """ thelcm = lcm(self.denom,f.denom) thediff = (thelcm/self.denom * self.numer) - \ (thelcm/f.denom * f.numer) return (thediff,thelcm) = (3,4) r2 = (1,2) r2 = r2 r2 = r2. sub () r2 r3 numer 5 denom 4 sub s N.S. self f thelcm thediff 27

28 Subtraction of s def sub (self, f): """return the difference """ thelcm = lcm(self.denom,f.denom) thediff = (thelcm/self.denom * self.numer) - \ (thelcm/f.denom * f.numer) return (thediff,thelcm) numer -1 = (3,4) r2 = (1,2) r2 = r2 r2 = r2. sub () r2 r3 numer 5 denom 4 sub s N.S. self f thelcm thediff Subtraction of s def sub (self, f): """return the difference """ thelcm = lcm(self.denom,f.denom) thediff = (thelcm/self.denom * self.numer) - \ (thelcm/f.denom * f.numer) return (thediff,thelcm) numer -1 = (3,4) r2 = (1,2) r2 = r2 r2 = r2. sub () r2 r3 numer 5 denom 4 28

29 Printing of s def str (self): """return the number as a string""" print('in str') return str(self.numer) + '/' + str(self.denom) numer -1 = (3,4) r2 = (1,2) r2 = r2 print(r2) r2 r3 numer 5 denom 4 Printing of s def str (self): """return the number as a string""" print('in str') return str(self.numer) + '/' + str(self.denom) numer -1 = (3,4) r2 = (1,2) r2 = r2 print(r2) print(r2. str() ) r2 r3 numer 5 denom 4 str s N.S. self 29

30 str v.s. repr str called by print and by str( ) repr called by shell Often the same: define str, have repr call str def repr (self): return self. str () Equality of s def eq (self, f): """compare the number and """ print('in eq') f1 = self.reduce() f2 = f.reduce() return f1.numer == f2.numer and \ f1.denom == f2.denom numer -1 if == r3: r2 r3 numer 5 denom 4 30

31 Equality of s def eq (self, f): """compare the number and """ print('in eq') f1 = self.reduce() f2 = f.reduce() return f1.numer == f2.numer and \ f1.denom == f2.denom numer -1 if == r3: if. eq (r3): r2 r3 numer 5 denom 4 eq s N.S. self f f1 f2 Operators (self,r2) sub (self,r2) (self,num,denom=1) str (self) repr (self) eq (self,r2) 31

32 eq and compares When we compare two fractions, we have to be sure that we are comparing the reduced form of the fraction For example, 1/2==2/4 should come back True Should we always reduce the result in our operations? Can you write? mul! div! lt! gt! 32

33 Problems to think about These don t work. Why? = (1,2) + 1 # fails, how to fix? 1 + # fails for a different # reason, how to fix? 33

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

What is a class? Responding to messages. Short answer 7/19/2017. Code Listing 11.1 First Class. chapter 11. Introduction to Classes chapter 11 Code Listing 11.1 First Class Introduction to Classes What is a class? If you have done anything in computer science before, you likely will have heard the term object oriented programming (OOP)

More information

CSC148 Intro. to Computer Science

CSC148 Intro. to Computer Science CSC148 Intro. to Computer Science Lecture 2: designing classes, special methods, managing attributes; intro composition, inheritance Amir H. Chinaei, Summer 2016 Office Hours: R 10 12 BA4222 csc148ta@cdf.toronto.edu

More information

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

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19 1 Classes 2 Exceptions 3 Using Other Code 4 Problems Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, 2009 1 / 19 Start with an Example Python is object oriented Everything is an object

More information

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

What is a Class? Short Answer. Responding to Messages. Everything in Python is an Object 11/8/2010 The Practice of Computing Using PYTHON William Punch Richard Enbody Chapter 11 Introduction to Classes class Student(object): """Simple Student class.""" def init (self,first='', last='', id=0): # init

More information

COMPSCI 105 S Principles of Computer Science. Classes 3

COMPSCI 105 S Principles of Computer Science. Classes 3 S2 2017 Principles of Computer Science Classes 3 Exercise } Exercise } Create a Student class: } The Student class should have three attributes: id, last_name, and first_name. } Create a constructor to

More information

Lecture #16: Generic Functions and Expressivity. Last modified: Fri Feb 26 19:16: CS61A: Lecture #16 1

Lecture #16: Generic Functions and Expressivity. Last modified: Fri Feb 26 19:16: CS61A: Lecture #16 1 Lecture #16: Generic Functions and Expressivity Last modified: Fri Feb 26 19:16:38 2016 CS61A: Lecture #16 1 Consider the function find: Generic Programming def find(l, x, k): """Return the index in L

More information

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

Recall. Key terms. Review. Encapsulation (by getters, setters, properties) OOP Features. CSC148 Intro. to Computer Science CSC148 Intro. to Computer Science Lecture 3: designing classes, special methods, composition, inheritance, Stack, Sack Amir H. Chinaei, Summer 2016 Office Hours: R 10-12 BA4222 ahchinaei@cs.toronto.edu

More information

ITP Programming Languages Chapter 2 Building Abstractions with Data 2.5 Object-Oriented Programming 1. Polymorphism repr, str Interface

ITP Programming Languages Chapter 2 Building Abstractions with Data 2.5 Object-Oriented Programming 1. Polymorphism repr, str Interface ITP 20005 Programming Languages Chapter 2 Building Abstractions with Data 2.5 Object-Oriented Programming 1. Polymorphism repr, str 2. Interface 3. @property 4. Example Complex number Major references:

More information

OBJECT ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING (download slides and.py files follow along!) 6.0001 LECTURE 8 6.0001 LECTURE 8 1 OBJECTS Python supports many different kinds of data 1234 3.14159 "Hello" [1, 5, 7, 11, 13]

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College September 6, 2017 Outline Outline 1 Chapter 2: Data Abstraction Outline Chapter 2: Data Abstraction 1 Chapter 2: Data Abstraction

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Haleh Ashki 2015, updated Peter Beerli 2017 Traditionally, a program has been seen as a recipe a set of instructions that you follow from start to finish in order to complete

More information

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

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1 Lecture #15: Generic Functions and Expressivity Last modified: Wed Mar 1 15:51:48 2017 CS61A: Lecture #16 1 Consider the function find: Generic Programming def find(l, x, k): """Return the index in L of

More information

String Representations

String Representations 61A Lecture 16 Announcements String Representations String Representations An object value should behave like the kind of data it is meant to represent For instance, by producing a string representation

More information

Object Oriented Programming #10

Object Oriented Programming #10 Object Oriented Programming #10 Serdar ARITAN Biomechanics Research Group, Faculty of Sports Sciences, and Department of Computer Graphics Hacettepe University, Ankara, Turkey 1 Simple programming tasks

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

Lecture 19. Operators and Abstraction

Lecture 19. Operators and Abstraction Lecture 19 Operators and Abstraction Announcements Reading Tuesday: Chapter 18 Thursday reading online Assignments A4 due tonight at Midnight 10 pts per day late Consultants available tonight A5 posted

More information

Computational Physics

Computational Physics Computational Physics Object-Oriented Programing Prof. Paul Eugenio Department of Physics Florida State University 26 Feb 2019 http://hadron.physics.fsu.edu/~eugenio/comphy/ Announcements Mid-Term 1 Will

More information

Computational Mathematics with Python

Computational Mathematics with Python Numerical Analysis, Lund University, 2015 1 Computational Mathematics with Python Unit 7: Object oriented programming with classes Numerical Analysis, Lund University Lecturer: Claus Führer 2015 Classes

More information

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

CS61A Lecture 21. Amir Kamil UC Berkeley March 11, 2013 CS61A Lecture 21 Amir Kamil UC Berkeley March 11, 2013 Announcements HW7 due on Wednesday Ants project out Looking Up Names Name expressions look up names in the environment Dot expressions look

More information

Iterators & Generators

Iterators & Generators Iterators & Generators Sequences A sequence is something that you can: Index into Get the length of What are some examples of sequences? Sequences We ve been working with sequences all semester! Examples:

More information

Object Oriented Programming. Feb 2015

Object Oriented Programming. Feb 2015 Object Oriented Programming Feb 2015 Tradi7onally, a program has been seen as a recipe a set of instruc7ons that you follow from start to finish in order to complete a task. That approach is some7mes known

More information

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

User Defined Types. Babes-Bolyai University Lecture 06. Lect Phd. Arthur Molnar. User defined types. Python scope and namespace ? User Defined Types Babes-Bolyai University arthur@cs.ubbcluj.ro Overview? 1? 2 3 ? NB! Types classify values. A type denotes a domain (a set of values) operations on those values. ? Object oriented programming

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College August 29, 2018 Outline Outline 1 Chapter 2: Data Abstraction Outline Chapter 2: Data Abstraction 1 Chapter 2: Data Abstraction

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Objectives To review the concepts and terminology of object-oriented programming To discuss some features of objectoriented design 1-2 Review: Objects In Java and other Object-Oriented

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Objectives To review the concepts and terminology of object-oriented programming To discuss some features of objectoriented design 1-2 Review: Objects In Java and other Object-Oriented

More information

61A LECTURE 14 MULTIPLE REPRESENTATIONS

61A LECTURE 14 MULTIPLE REPRESENTATIONS 61A LECTURE 14 MULTIPLE REPRESENTATIONS Steven Tang and Eric Tzeng July 17, 2013 Generic Functions An abstrac/on might have more than one representa/on. Python has many sequence types: tuples, ranges,

More information

Lecture 3. Input, Output and Data Types

Lecture 3. Input, Output and Data Types Lecture 3 Input, Output and Data Types Goals for today Variable Types Integers, Floating-Point, Strings, Booleans Conversion between types Operations on types Input/Output Some ways of getting input, and

More information

61A LECTURE 14 MULTIPLE REPRESENTATIONS. Steven Tang and Eric Tzeng July 17, 2013

61A LECTURE 14 MULTIPLE REPRESENTATIONS. Steven Tang and Eric Tzeng July 17, 2013 61A LECTURE 14 MULTIPLE REPRESENTATIONS Steven Tang and Eric Tzeng July 17, 2013 Generic Functions An abstrac*on might have more than one representa*on. Python has many sequence types: tuples, ranges,

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

Object-Oriented Programming Concepts

Object-Oriented Programming Concepts Object-Oriented Programming Concepts Real world objects include things like your car, TV etc. These objects share two characteristics: they all have state and they all have behavior. Software objects are

More information

C++ & Object Oriented Programming Concepts The procedural programming is the standard approach used in many traditional computer languages such as BASIC, C, FORTRAN and PASCAL. The procedural programming

More information

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013 Expressions and Casting C# Programming Rob Miles Data Manipulation We know that programs use data storage (variables) to hold values and statements to process the data The statements are obeyed in sequence

More information

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

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017 Overview of OOP Dr. Zhang COSC 1436 Summer, 2017 7/18/2017 Review Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in square brackets: l = [1, 2, "a"] (access by index, is mutable

More information

Computational Mathematics with Python

Computational Mathematics with Python Numerical Analysis, Lund University, 2012 1 Computational Mathematics with Python Unit 8: Object oriented programming with classes Numerical Analysis, Lund University Claus Führer, Olivier Verdier, Tony

More information

20. More Complicated Classes. A Class For Manipulating Fractions. A Class For Manipulating Fractions 4/19/2016. Let s Define a Class to Do This Stuff

20. More Complicated Classes. A Class For Manipulating Fractions. A Class For Manipulating Fractions 4/19/2016. Let s Define a Class to Do This Stuff 4/9/26 2. More Complicated Classes Class For Manipulating Fractions Topics: Example: The class Fraction Operator Overloading Class Invariants Example: The class SimpleDate Class Variables deepcopy You

More information

Expressions and Casting

Expressions and Casting Expressions and Casting C# Programming Rob Miles Data Manipulation We know that programs use data storage (variables) to hold values and statements to process the data The statements are obeyed in sequence

More information

Lecture 19. Using Classes Effectively

Lecture 19. Using Classes Effectively Lecture 19 Using Classes Effectively Announcements Reading Tuesday: Chapter 18 Thursday reading online Assignments A4 due tonight at Midnight 10 pts per day late Consultants available tonight A5 & A6 posted

More information

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes GIS 4653/5653: Spatial Programming and GIS More Python: Statements, Types, Functions, Modules, Classes Statement Syntax The if-elif-else statement Indentation and and colons are important Parentheses and

More information

Creating a new data type

Creating a new data type Appendix B Creating a new data type Object-oriented programming languages allow programmers to create new data types that behave much like built-in data types. We will explore this capability by building

More information

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A DEBUGGING TIPS COMPUTER SCIENCE 61A 1 Introduction Every time a function is called, Python creates what is called a stack frame for that specific function to hold local variables and other information.

More information

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi CMPT 120 Basics of Python Summer 2012 Instructor: Hassan Khosravi Python A simple programming language to implement your ideas Design philosophy emphasizes code readability Implementation of Python was

More information

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

ECE 364 Software Engineering Tools Laboratory. Lecture 7 Python: Object Oriented Programming ECE 364 Software Engineering Tools Laboratory Lecture 7 Python: Object Oriented Programming 1 Lecture Summary Object Oriented Programming Concepts Object Oriented Programming in Python 2 Object Oriented

More information

Unit Tests. # Store the result of a boolean expression in a variable. >>> result = str(5)=='5'

Unit Tests. # Store the result of a boolean expression in a variable. >>> result = str(5)=='5' 7 Unit Testing Lab Objective: Finding and fixing programming errors can be difficult and time consuming, especially in large or complex programs. Unit testing is a formal strategy for finding and eliminating

More information

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017 SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

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

COMP519 Web Programming Lecture 21: Python (Part 5) Handouts COMP519 Web Programming Lecture 21: Python (Part 5) Handouts Ullrich Hustadt Department of Computer Science School of Electrical Engineering, Electronics, and Computer Science University of Liverpool Functions

More information

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0)) SCHEME 0 COMPUTER SCIENCE 6A July 26, 206 0. Warm Up: Conditional Expressions. What does Scheme print? scm> (if (or #t (/ 0 (/ 0 scm> (if (> 4 3 (+ 2 3 4 (+ 3 4 (* 3 2 scm> ((if (< 4 3 + - 4 00 scm> (if

More information

Introduction to Python

Introduction to Python Introduction to Python Reading assignment: Perkovic text, Ch. 1 and 2.1-2.5 Python Python is an interactive language. Java or C++: compile, run Also, a main function or method Python: type expressions

More information

CSE 341, Autumn 2015, Ruby Introduction Summary

CSE 341, Autumn 2015, Ruby Introduction Summary CSE 341, Autumn 2015, Ruby Introduction Summary Disclaimer: This lecture summary is not necessarily a complete substitute for atting class, reading the associated code, etc. It is designed to be a useful

More information

At full speed with Python

At full speed with Python At full speed with Python João Ventura v0.1 Contents 1 Introduction 2 2 Installation 3 2.1 Installing on Windows............................ 3 2.2 Installing on macos............................. 5 2.3

More information

Type Checking. Error Checking

Type Checking. Error Checking Type Checking Error Checking Dynamic checking takes place while program is running Static checking takes place during compilation Type checks Flow-of-control checks Uniqueness checks Name-related checks

More information

Glossary. For Introduction to Programming Using Python By Y. Daniel Liang

Glossary. For Introduction to Programming Using Python By Y. Daniel Liang Chapter 1 Glossary For Introduction to Programming Using Python By Y. Daniel Liang.py Python script file extension name. assembler A software used to translate assemblylanguage programs into machine code.

More information

Microsoft Visual Basic 2005: Reloaded

Microsoft Visual Basic 2005: Reloaded Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 10 Creating Classes and Objects Objectives After studying this chapter, you should be able to: Define a class Instantiate an object from a class

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

Chapter 1. Fundamentals of Higher Order Programming

Chapter 1. Fundamentals of Higher Order Programming Chapter 1 Fundamentals of Higher Order Programming 1 The Elements of Programming Any powerful language features: so does Scheme primitive data procedures combinations abstraction We will see that Scheme

More information

CNRS ANF PYTHON Objects everywhere

CNRS ANF PYTHON Objects everywhere CNRS ANF PYTHON Objects everywhere Marc Poinot Numerical Simulation Dept. Outline Python Object oriented features Basic OO concepts syntax More on Python classes multiple inheritance reuse introspection

More information

Working with Strings. Husni. "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.

Working with Strings. Husni. The Practice of Computing Using Python, Punch & Enbody, Copyright 2013 Pearson Education, Inc. Working with Strings Husni "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Sequence of characters We've talked about strings being a sequence of characters.

More information

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

CS304 Object Oriented Programming Final Term

CS304 Object Oriented Programming Final Term 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing

More information

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme

More information

Getting Started with Python

Getting Started with Python Fundamentals of Programming (Python) Getting Started with Python Sina Sajadmanesh Sharif University of Technology Some slides have been adapted from Python Programming: An Introduction to Computer Science

More information

What is a Fraction? Fractions. One Way To Remember Numerator = North / 16. Example. What Fraction is Shaded? 9/16/16. Fraction = Part of a Whole

What is a Fraction? Fractions. One Way To Remember Numerator = North / 16. Example. What Fraction is Shaded? 9/16/16. Fraction = Part of a Whole // Fractions Pages What is a Fraction? Fraction Part of a Whole Top Number? Bottom Number? Page Numerator tells how many parts you have Denominator tells how many parts are in the whole Note: the fraction

More information

A Paradigm (para-dime) is a framework containing the basic assumptions, ways of thinking, and methodology that are commonly accepted by members of a

A Paradigm (para-dime) is a framework containing the basic assumptions, ways of thinking, and methodology that are commonly accepted by members of a A Paradigm (para-dime) is a framework containing the basic assumptions, ways of thinking, and methodology that are commonly accepted by members of a scientific community. So far this semester, we've been

More information

CSCA20 Worksheet Strings

CSCA20 Worksheet Strings 1 Introduction to strings CSCA20 Worksheet Strings A string is just a sequence of characters. Why do you think it is called string? List some real life applications that use strings: 2 Basics We define

More information

OBJECT ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism http://en.wikipedia.org/wiki/encapsulation_(object-oriented_programming)

More information

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and Classes CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

CS 251 Intermediate Programming Methods and More

CS 251 Intermediate Programming Methods and More CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

COMP 202 Java in one week

COMP 202 Java in one week CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator COMP 202 Java in one week The Java Programming Language A programming language

More information

CS1210 Lecture 28 Mar. 27, 2019

CS1210 Lecture 28 Mar. 27, 2019 CS1210 Lecture 28 Mar. 27, 2019 Discussion section exam scores posted score # people 0-5 6-10 11-15 16-20 21-25 26-30 28 48 39 37 30 9 median: 13 Some words about overall grades more detail next Wednesday

More information

Algorithms and Programming I. Lecture#12 Spring 2015

Algorithms and Programming I. Lecture#12 Spring 2015 Algorithms and Programming I Lecture#12 Spring 2015 Think Python How to Think Like a Computer Scientist By :Allen Downey Installing Python Follow the instructions on installing Python and IDLE on your

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Java basics: Compilation vs Interpretation Program structure Statements Values Variables Types Operators and Expressions

More information

PREPARING FOR PRELIM 2

PREPARING FOR PRELIM 2 PREPARING FOR PRELIM 2 CS 1110: FALL 2012 This handout explains what you have to know for the second prelim. There will be a review session with detailed examples to help you study. To prepare for the

More information

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University Day 3 COMP 1006/1406A Summer 2016 M. Jason Hinek Carleton University today s agenda assignments 1 was due before class 2 is posted (be sure to read early!) a quick look back testing test cases for arrays

More information

PYTHON. Values and Variables

PYTHON. Values and Variables December 13 2017 Naveen Sagayaselvaraj PYTHON Values and Variables Overview Integer Values Variables and Assignment Identifiers Floating-point Types User Input The eval Function Controlling the print Function

More information

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

More information

Lecture 18: Using Classes Effectively (Chapter 16)

Lecture 18: Using Classes Effectively (Chapter 16) http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 18: Using Classes Effectively (Chapter 16) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner,

More information

DATA ABSTRACTION 5. February 21, 2013

DATA ABSTRACTION 5. February 21, 2013 DATA ABSTRACTION 5 COMPUTER SCIENCE 61A February 21, 2013 1 Data Abstraction Data abstraction is a powerful concept in computer science that allows programmers to treat code as objects for example, car

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement Outline Expression Evaluation and Control Flow In Text: Chapter 6 Notation Operator evaluation order Operand evaluation order Overloaded operators Type conversions Short-circuit evaluation of conditions

More information

OBJECT ORİENTATİON ENCAPSULATİON

OBJECT ORİENTATİON ENCAPSULATİON OBJECT ORİENTATİON Software development can be seen as a modeling activity. The first step in the software development is the modeling of the problem we are trying to solve and building the conceptual

More information

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT Python The way of a program Srinidhi H Asst Professor Dept of CSE, MSRIT 1 Problem Solving Problem solving means the ability to formulate problems, think creatively about solutions, and express a solution

More information

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines. Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

A Little Python Part 1. Introducing Programming with Python

A Little Python Part 1. Introducing Programming with Python A Little Python Part 1 Introducing Programming with Python Preface Not a complete course in a programming language Many details can t be covered Need to learn as you go My programming style is not considered

More information

Programmazione. Prof. Marco Bertini

Programmazione. Prof. Marco Bertini Programmazione Prof. Marco Bertini marco.bertini@unifi.it http://www.micc.unifi.it/bertini/ Introduction Why OO Development? Improved structure of software easier to: Understand Maintain Enhance Reusable

More information

Postscript Control Flow

Postscript Control Flow Postscript Control Flow CSE 413, Autumn 2005 Programming Languages http://www.cs.washington.edu/education/courses/413/05au/ Variables Postscript uses dictionaries to associate a name with an object value»

More information

Expressions, Statements, Variables, Assignments, Types

Expressions, Statements, Variables, Assignments, Types Expressions, Statements, Variables, Assignments, Types CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of this material

More information

Outline. Outline. 1 Chapter 2: Data Abstraction

Outline. Outline. 1 Chapter 2: Data Abstraction Outline Outline 1 Chapter 2: Data Abstraction From Data Type to ADT Values A value is a unit of information used in a program. It can be associated with a constant or variable (a name) by an assignment

More information

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion CSC 148 Lecture 3 Dynamic Typing, Scoping, and Namespaces Recursion Announcements Python Ramp Up Session Monday June 1st, 1 5pm. BA3195 This will be a more detailed introduction to the Python language

More information

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003 Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 Java Programs A Java program contains at least one class definition. public class Hello { public static void

More information

A Java program contains at least one class definition.

A Java program contains at least one class definition. Java Programs Identifiers Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 A Java program contains at least one class definition. public class Hello { public

More information

WELCOME! (download slides and.py files and follow along!) LECTURE 1

WELCOME! (download slides and.py files and follow along!) LECTURE 1 WELCOME! (download slides and.py files and follow along!) 6.0001 LECTURE 1 6.0001 LECTURE 1 1 TODAY course info what is computation python basics mathematical operations python variables and types NOTE:

More information

BASIC CONCEPT OF OOP

BASIC CONCEPT OF OOP Chapter-6 BASIC CONCEPT OF OOP Introduction: Object oriented programmingg is the principle of design and development of programs using modular approach. Object oriented programmingg approach provides advantages

More information

THE REAL NUMBER SYSTEM

THE REAL NUMBER SYSTEM THE REAL NUMBER SYSTEM Review The real number system is a system that has been developing since the beginning of time. By now you should be very familiar with the following number sets : Natural or counting

More information

SCHEME AND CALCULATOR 5b

SCHEME AND CALCULATOR 5b SCHEME AND CALCULATOR 5b COMPUTER SCIENCE 6A July 25, 203 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types

More information

CSCI 121: Anatomy of a Python Script

CSCI 121: Anatomy of a Python Script CSCI 121: Anatomy of a Python Script Python Scripts We start by a Python script: A text file containing lines of Python code. Each line is a Python statement. The Python interpreter (the python3 command)

More information

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors CMSC201 Computer Science I for Majors Lecture 25 Classes All materials copyright UMBC and Dr. Katherine Gibson unless otherwise noted Run time Last Class We Covered Run time of different algorithms Selection,

More information

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming 2017 Table of contents 1 2 Integers and floats Integer int and float float are elementary numeric types in. integer >>> a=1 >>> a 1 >>> type (a) Integers and floats Integer int and float

More information

2. First Program Stuff

2. First Program Stuff CSE 232 First Midterm, Overview: 1. Getting Started 1. we got started 2. First Program Stuff 1. Compiler vs. Intepreter a. do you know all the steps to create an executable? 2. Variables are declared a.

More information

Operator Overloading. a flop = a floating-point operation overloading arithmetical operators counting number of flops in a sum

Operator Overloading. a flop = a floating-point operation overloading arithmetical operators counting number of flops in a sum Operator Overloading 1 OOP to count Flops a flop = a floating-point operation overloading arithmetical operators counting number of flops in a sum 2 Quaternions hypercomplex numbers application in computer

More information

FUNCTIONAL PROGRAMMING NO.9 TYPE AND CLASS. Tatsuya Hagino

FUNCTIONAL PROGRAMMING NO.9 TYPE AND CLASS. Tatsuya Hagino 1 FUNCTIONAL PROGRAMMING NO.9 TYPE AND CLASS Tatsuya Hagino hagino@sfc.keio.ac.jp 2 Static Type Checking and Type Inference Type a set of values Bool = { True, False } Char = { 'a', 'b',... } Int = {...

More information