#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Felix Muzny 11/15/2022 DS 2000 Lecture 19 - classes and objects Logistics: - Homework 8 is out - this is your last HW for DS 2000 - there is a significant amount of extra credit available - due on Friday - OH this week - 2 - 8pm today - I'll also be devoting time in lecture to HW 8 questions - Next week: - yes, we have class on 11/22 - yes, there will be a remote option - yes, "I'm out of town" is a valid reason to attend remotely - yes, I do expect you to attend :) - There is a quiz this week, due before class on Friday - https://www.gradescope.com/courses/408909/assignments/2392869 - remote attendance (https://bit.ly/remote-ds2000-muzny) Three ways to participate in multiple choice questions 1) via the PollEverywhere website: https://pollev.com/muzny 2) via text: text "muzny" to the number 22333 to join the session 3) via Poll Everywhere app (available for iOS or Android) """ """ Classes and Objects --- Warning! vocabulary ahead! It is often useful for us to use *abstraction* as programmers. For example, we use `list`s in our code. Does the computer actually have a list that it has created? Animals: - cat - dog - ferret - turtle animals = ["cat", "dog", "ferret", "turtle"] # care that the computer can answer some questions # consistently with the behavior that we expect -> how many things are in the list? -> what is the first thing? -> what is the last thing? -> what order are they in? -> how many _____s are in the list? animal_name = "turtle" animal_legs = 4 has_tail = True animal2_name = "giraffe" animal2_legs = 4 has2_tail = True animal3_name = "spider" animal3_legs = 8 has3_tail = False The typical way that we approach abstraction (across many languages) is through classes. class: a blue print of some *thing* that we want to represent -> what defines an Animal in general? -> kind, # of legs, have a tail, etc.... object: a specific *instance* of a class -> this is a turtle, giraffe, spider """ # list is a class # this list is an object # it is an instance of the list class numbers = [3, -5, 17] print(type(numbers)) """ For example, if the class is `Professor`, then `felix = Professor("Felix")` would be the instance; `felix` is an object of type `Professor`. """ # right now, we're pretty much stuck with: prof_name = "Felix" prof_age = 31 prof_subj = "Computer Science" """ Class design --- Whenever we want to design a class, we want to answer two questions: 1. What traits does this thing *have*? - name - age - subject - years of experience - rating number - pronouns - courses taught These things will become class attributes. 2. What should this thing be able to *do*? (verbs) - teach a course - grade an assignment - hold office hours - answer a question - answer regrade request - grant and extension - greet a student These things will become methods. """ """ Attributes & Methods --- An attribute is like a variable that belongs to a specific object. A method is like a function that belongs to a particular class. """ """ Syntax --- Define a class with the code class ClassName: # always put "self" for the first parameter # we'll talk about why on Friday def method_name(self, params): # code """ # An example class # this tells python "I'm defining a class now!" class Professor: # constructor def __init__(self, prof_name): print("Create a professor") # set up code for your class attributes # self.attribute = value self.name = prof_name # no courses taught to begin with self.courses_taught = [] def greet(self): print("Hello, I'm a professor") def teach(self, course_name): # whenever I access an attribute, I need to use self self.courses_taught.append(course_name) # create an object # call the constructor to create a Professor # you need to match parameters with everything AFTER self felix = Professor("Felix") # to call a method felix.greet() felix.teach("DS 2000") felix.teach("CS 4120") # felix.teach(["DS 2500", "CS 2810"]) # to access an attribute print("Felix's name:", felix.name) print("Felix's courses:", felix.courses_taught) print(type(felix)) print() yoda = Professor("Yoda") yoda.greet() yoda.teach("the force") print("Yoda's name:", yoda.name) print("Yoda's courses:", yoda.courses_taught) print(type(yoda)) print() """ Instantiating objects --- Wait, but how are objects created??? Like, in the code? A special method called a constructor is called when you want to create an object (or "instantiate a class"). Inside your class: def __init__(self, params): # code to set up attributes Then, later: variable = ClassName(parameters AFTER self) """ # what is the data type of felix? (or yoda) # Professor # how do you access attributes of an object? # variable.attribute # felix.name # how do you call methods of an object? # variable.method(params AFTER self) # felix.greet() """ Wait, what have we done? --- - learned how to create *custom data types* by defining our own python classes - learned how to instantiate python classes - made our world ever more complex Every data type in python is a class—you've been using these all along! In particular, any data type that you can call a function on is a class! - strings (str.lower()) - lists - dicts - sets """ """ Cool-downs/examples that *might* be helpful for HW 8 --- """ # 1 - what does the following code print? # each character in the line, not each word line = "We'll be DESTROYED for sure" for word in line: print(word) # 2 - what edits can you make to the following code to fix it? # you need to split the line on whitespace line = "We'll be DESTROYED for sure" line = line.split() for word in line: print(word) # 3 - will "found one!" ever be printed? # if not, fix this code so that it will # DESTROYED is not lowercase, so this won't pass line = "We'll be DESTROYED for sure" line = line.split() key_words = ["destroyed", "killed", "ruined"] for word in line: word = word.lower() if word in key_words: print("found one!") # 4 - edit this code so that it prints each word # in lower case, with ' removed line = "We'll be DESTROYED for sure" line = line.split() for word in line: word = word.lower() word = word.replace("'", "") print(word) """ The rest of class today --- 1. Work time for HW 8 - if you have finished loading in your data *and* calculated the sentiment score for one line of dialogue, you are free to go/take care of what you need to do. - else, please stay here and get that data loaded! -> if you're stuck, read piazza, post a question, or ask me a question! 2. If you haven't completed your TRACE reports, please do. Here's why: - I *do* read these reports to get feedback and to adjust courses - I have two asks for you: -> if there are things that you liked, please tell me, so that I can keep them! -> if there are things that you would like to see changed, please be specific, so that I can make adjustments! (- finally, these reports *are* used by university administration/my bosses) """ # Next Time # --- # - Pokemon program # - more classes/objects