More On Classes UW CSE 160 Spring 2015 1. Classes define objects What are objects we've seen? 2.

Slides:



Advertisements
Similar presentations
Topics in Python Blackjack & TKinter
Advertisements

Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
Data Abstraction UW CSE 190p Summer Recap of the Design Exercise You were asked to design a module – a set of related functions. Some of these functions.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
I210 review (for final exam) Fall 2011, IUB. What’s in the Final Exam Multiple Choice (5) Short Answer (5) Program Completion (3) Note: A single-sided.
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
Floating point numbers in Python Floats in Python are platform dependent, but usually equivalent to an IEEE bit C “double” However, because the significand.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING DATA ABSTRACTION Jehan-François Pâris
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Computer Science 111 Fundamentals of Programming I More Data Modeling.
More Data Abstraction UW CSE 190p Summer Recap of the Design Exercise You were asked to design a module – a set of related functions. Some of these.
Data Abstraction UW CSE 140 Winter What is a program? – A sequence of instructions to achieve some particular purpose What is a library? – A collection.
Guide to Programming with Python
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
Design Exercise UW CSE 140 Winter Exercise Given a problem description, design a module to solve the problem 1) Specify a set of functions – For.
Computer Science 111 Fundamentals of Programming I Model/View/Controller and Data model design.
Computer Science 112 Fundamentals of Programming II Interfaces and Implementations.
Guide to Programming with Python Week 11 Chapter Nine Inheritance Working with multiple objects.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Design Exercise UW CSE 190p Summer 2012 download examples from the calendar.
November 28, 2005ICP: Chapter 9: Object Oriented Programming 1 Introduction to Computer Programming Chapter 9: Object Oriented Programming Michael Scherger.
Extra Stuff for CS106 Victor Norman CS106. break and continue break and continue are both ways to alter the flow of a loop Can be used only inside a loop.
Design Exercise UW CSE 160 Spring Exercise Given a problem description, design a module to solve the problem 1) Specify a set of functions – For.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:
Python Let’s get started!.
Fall 2015, Kevin Quinn CSE373: Data Structures & Algorithms Lecture 25: Problem Solving CSE373: Data Structures and algorithms1.
Chapter 17 Q and A Victor Norman, et al. CS104. What is Object-oriented Programming? Q: What is object-oriented programming? A: It means defining classes/objects,
Q and A for Sections 6.2, 6.3 Victor Norman CS106.
Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Data Abstraction UW CSE 160 Spring What is a program? – A sequence of instructions to achieve some particular purpose What is a library? – A collection.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
Input, Output and Variables GCSE Computer Science – Python.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Python programming - Defining Classes
COMPSCI 107 Computer Science Fundamentals
Guide to Programming with Python
More On Classes UW CSE 160 Winter
COMPSCI 107 Computer Science Fundamentals
Python Let’s get started!.
CSSE 120—Rose Hulman Institute of Technology
Software Development Java Classes and Methods
CS-104 Final Exam Review Victor Norman.
Case Statements and Functions
Computer Science 111 Fundamentals of Programming I
Fundamentals of Programming I More Data Modeling
Design Exercise UW CSE 140 Winter 2014.
Design Exercise UW CSE 160 Winter 2017.
Data Abstraction UW CSE 160 Winter 2017.
Data Abstraction UW CSE 160 Winter 2016.
Design Exercise UW CSE 160 Spring 2018.
Data Abstraction UW CSE 160 Spring 2018.
Computer Science 111 Fundamentals of Programming I
Fundamentals of Programming I More Data Modeling
Design Exercise UW CSE 160 Winter 2016.
A Level Computer Science Topic 6: Introducing OOP
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Data Types and Maths Programming Guides.
Hash Maps Implementation and Applications
Migrating to Object-Oriented Programs
Data Abstraction UW CSE 160.
Presentation transcript:

More On Classes UW CSE 160 Spring

Classes define objects What are objects we've seen? 2

Classes define objects What are objects we've seen? String Int Float Dict List Set Graph 3 File CSV Writer Others?

Objects can be created  set_one = set()  dict_one = dict() # dict_one = {}  str_one = str() # str_one = ''  list_one = list() # list_one = []  file_one = open('data.csv')  import networkx as nx  graph_one = nx.Graph() 4

Objects have methods  set_one.append('purple')  dict_one.setdefault('four',16)  str_one.capitalize()  list_one.extend([1,2,3,4])  graph_one.add_edges ([(1,2),(1,3),(2,4)]) 5

Objects have internal state 6 str_one = 'purple' str_two = 'spectrographically' >> str_one.count('c') 0 >> str_two.count('c') 2 >> graph_one.nodes() [1,2,3,4]

Classes define objects A class is a blueprint for an object. 7 class Vehicle: Style Note: Classes use CamelCase. No spaces or underscore but the first letter of each word is capitalized. Usually keep class names to a single word if possible.

Classes define objects A class is a blueprint for an object. 8 class Vehicle: def __init__(self, make, color, passengers, wheels=4, tank=20): ''' Create a new Vehicle Object ''' self.model, self.color, self.wheels = make, color, wheels self.seats = passengers self.gas = 0 if __name__ == '__main__': my_car = Vehicle('Honda', 'White', 4) your_motorcycle = Vehicle('Mazda', 'Red', 2, 2) semi = Vehicle('Mercedes', 'Black', 2, wheels=16)

Classes define objects A class is a blueprint for an object. 9 class Vehicle: def __init__(self, make, color, passengers, wheels=4, tank=20): ''' Create a new Vehicle Object ''' self.model, self.color = make, color self.seats = passengers self.wheels, self.tank = wheels, tank self.gas = 0 if __name__ == '__main__': my_car = Vehicle('Honda', 'White', 4) your_motorcycle = Vehicle('Mazda', 'Red', 2, 2) semi = Vehicle('Mercedes', 'Black', 2, wheels=16) __init__ is the constructor. This is a “magic” method. Means something special to python. In this case it defines how to create a new Vehicle object.

Classes define objects 10 class Vehicle: def __init__(self, make, color, passengers, wheels=4, tank=20): ''' Create a new Vehicle Object ''' self.model, self.color = make, color self.seats = passengers self.wheels, self.tank = wheels, tank self.gas = 0 def fill_tank(self,gallons): '''Add gallons to tank. Until it is full''' self.gas += gallons if self.gas > self.tank : self.gas = self.tank

Classes define objects 11 class Vehicle: def __init__(self, make, color, passengers, wheels=4, tank=20): ''' Create a new Vehicle Object ''' self.model, self.color = make, color self.seats = passengers self.wheels, self.tank = wheels, tank self.gas = 0 def __str__(self): return 'Gas remaining: ' + str(self.gas) def __hash__(self): return hash(self.make) + hash(self.color) + hash(self.seats) +\ hash(self.wheels) + hash(self.tank) + hash(self.gas) More “magic” methods to convert object to a string or hash.

Let's Play With Vehicles import vehicle 12

Why Use Classes Classes are blueprints for objects, objects model the real world. This makes programming easier. Have multiple objects with similar functions (methods) but different internal state. Provide a software abstraction for clients to use without needing to know the details of your program. 13

Why Use Classes 14 class Pizza: def __init__(self, name, toppings): self.name, self.toppings = name,toppings def is_vegetarian(self): for t in self.toppings: if not t.vegetarian: return False else return True class Topping: def __init__(self, name, veg=False): self.name = name self.vegetarian = veg

Why Use Classes 15 #make toppings from pizza import * cheese, tomato = Topping('cheese',True), Topping('tomato',True) pepper, pineapple = Topping('pepper',True), Topping('pineapple',True) pepperoni, ham = Topping('pepperoni'), Topping('ham') cheese_pizza = Pizza('cheese',[cheese,tomato]) hawaiian = Pizza('hawaiian',[cheese,tomato,pineapple,ham]) combo = Pizza('combo',[cheese,tomato,pepper,pineapple]) >> combo.is_vegetarian() True >> hawaiian.is_vegetarian() False

def read_words(filename): """Return dictionary mapping each word in filename to its frequency.""" wordfile = open(filename) word_list = wordfile.read().split() wordfile.close() wordcounts_dict = {} for word in word_list: count = wordcounts_dict.setdefault(word, 0) wordcounts_dict[word] = count + 1 return wordcounts_dict def word_count(wordcounts_dict, word): """Return count of the word in the dictionary. """ if wordcounts_dict.has_key(word): return wordcounts_dict[word] else: return 0 def topk(wordcounts_dict, k=10): """Return list of (count, word) tuples of the top k most frequent words.""" counts_with_words = [(c, w) for (w, c) in wordcounts_dict.items()] counts_with_words.sort(reverse=True) return counts_with_words[0:k] def total_words(wordcounts_dict): """Return the total number of words.""" return sum(wordcounts_dict.values()) Text analysis module (group of related functions) representation = dictionary # program to compute top 5: wordcounts = read_words(filename) result = topk(wordcounts, 5) 16

Problems with the implementation The wordcounts dictionary is exposed to the client: the user might corrupt or misuse it. If we change our implementation (say, to use a list), it may break the client program. We prefer to – Hide the implementation details from the client – Collect the data and functions together into one unit # program to compute top 5: wordcounts = read_words(filename) result = topk(wordcounts, 5) 17

Class Implementation class WordCounts: """Represents the words in a file.""" # Internal representation: # variable wordcounts is a dictionary mapping words to frequency def __init__(self, filename): """Create a WordCounts object from the given file""" words = open(filename).read().split() self.wordcounts = {} for w in words: self.wordcounts.setdefault(w, 0) self.wordcounts[w] += 1 def word_count(self, word): """Return the count of the given word""" return self.wordcounts[word] def topk(self, k=10): """Return a list of the top k most frequent words in order""" scores_with_words = [(c,w) for (w,c) in self.wordcounts.items()] scores_with_words.sort(reverse=True) return scores_with_words[0:k] def total_words(self): """Return the total number of words in the file""" return sum([c for (w,c) in self.wordcounts]) # program to compute top 5: wc = WordCounts(filename) result = wc.topk(5) 18

Alternate implementation class WordCounts: """Represents the words in a file.""" # Internal representation: # variable words is a list of the words in the file def __init__(self, filename): """Create a WordCounts object from the given file""" self.words = open(filename).read().split() def word_count(self, word): """Return the count of the given word""" return self.words.count(word) def topk(self, k=10): """Return a list of the top k most frequent words in order""" scores_with_words = [(self.wordcount(w),w) for w in set(self.words)] scores_with_words.sort(reverse=True) return scores_with_words[0:k] def total_words(self): """Return the total number of words in the file""" return len(self.words) # program to compute top 5: wc = WordCounts(filename) result = wc.topk(5) Exact same program!

A Card Game Create the base classes that could be used by a client to create multiple card games. Blackjack Spades Poker Cribbage Euchre (24 cards!) 20

A Card Game: Design What are some high level classes that might be useful? 21

A Card Game: Design What are some high level classes that might be useful? Deck Holds a set of cards, can be shuffled and deal cards into Hands. Hand Holds cards and has basic methods for calculating properties. (has pair, sum ect) Card Takes a face value character, points value, and suit. 22

A Card Game: Design Useful functions for Card class 23 class Card:

A Card Game: Design Useful functions for Card class 24 class Card: def __init__(self, face, suit, value=1): '''Create a new card''' self.face, self.suit, = face.upper()[0], suit.upper()[0] self.value = value def is_black(self): return self.suit == 'S' or self.suit == 'C' def is_face(self): return not self.face.isdigit()

A Card Game: Design More magic methods, comparing cards 25 (Also in class Card:) … def __eq__(self,other): return self.value == other.value def __lt__(self,other): return self.value < other.value def __gt__(self,other): return self.value > other.value See Also: __ne__, __le__, __ge__

A Card Game: Design Useful functions for the Hand class 26 class Hand:

A Card Game: Design Useful functions for the Hand class 27 class Hand: def __init__(self,cards): self.card = cards def value(self): return sum([c.value for c in self.cards]) def has_pair(self): '''Returns True if hand has a pair''' for i,c in enumerate(self.cards): for c2 in self.cards[i+1:]: if c.face == c2.face: return True return False

A Card Game: Design Useful functions for the Deck class 28 class Deck:

A Card Game: Design Useful functions for the Deck class 29 class Deck: def __init__(self,cards): self.cards = cards def shuffle(self): '''Randomize the order of internal cards list''' random.shuffle(self.cards) def deal(self,n=1): hand_cards = self.cards[0:n] del self.cards[0:n] return Hand(hand_cards)

A Card Game: Design Useful functions for the Deck class 30 (also in class Deck:) … def __len__(self): return len(self.cards)