#!/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? 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 object: a specific *instance* of a class 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: """ Class design --- Whenever we want to design a class, we want to answer two questions: 1. What traits does this thing *have*? These things will become class attributes. 2. What should this thing be able to *do*? 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: def method_name(self, params): # code """ # An example class # this tells python "I'm defining a class now!" """ 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: Then, later: variable = ClassName(parameters AFTER self) """ # what is the data type of felix? # how do you access attributes of an object? # how do you call methods of an object? """ 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 - lists - dicts - sets """ """ Cool-downs/examples that *might* be helpful for HW 8 --- """ # # 1 - what does the following code print? # 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? # line = "We'll be DESTROYED for sure" # for word in line: # print(word) # # 3 - will "found one!" ever be printed? # # if not, fix this code so that it will # line = "We'll be DESTROYED for sure" # key_words = ["destroyed", "killed", "ruined"] # for word in line: # 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" # for word in line: # 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