CS104: Chapter 15
description
Transcript of CS104: Chapter 15
![Page 1: CS104: Chapter 15](https://reader036.fdocuments.net/reader036/viewer/2022082612/56813d2b550346895da6f0f1/html5/thumbnails/1.jpg)
CS104: Chapter 15
CS 104 Students and me
![Page 2: CS104: Chapter 15](https://reader036.fdocuments.net/reader036/viewer/2022082612/56813d2b550346895da6f0f1/html5/thumbnails/2.jpg)
Big Question
Q: You say creating a class is how to create a new type. Why would we even want to do this?!
A: First, to group data together that belongs together. (Second, to group operations on the data with the data. But, more on that, in due course.)
![Page 3: CS104: Chapter 15](https://reader036.fdocuments.net/reader036/viewer/2022082612/56813d2b550346895da6f0f1/html5/thumbnails/3.jpg)
Grouping Data
• The only data structures we have are lists and tuples.
• We can store multiple related data items together in a list or tuple, and then keep a list of these lists/tuples.
• E.g., cars = [ (“Honda”, “Odyssey”, 2001, “silver”, “VIN1523y7380”), (“Toyota”, “Prius”, 2007, “blue”, “VIN99383829X95”), …]
![Page 4: CS104: Chapter 15](https://reader036.fdocuments.net/reader036/viewer/2022082612/56813d2b550346895da6f0f1/html5/thumbnails/4.jpg)
Accessing Data
• If we need to compute with the i-th car’s color, we do:car = cars[i] # car is a tuple if car[3] == “blue”: # do something…
• Or, was it car[4]? car[5]? If we have 55 items to keep for a car how do we remember which number is in which place in the tuple of a car? How can we make the code readable?
![Page 5: CS104: Chapter 15](https://reader036.fdocuments.net/reader036/viewer/2022082612/56813d2b550346895da6f0f1/html5/thumbnails/5.jpg)
A better way
• Would be nice to make a container to hold multiple data items, and each attribute in the structure has a nice name.
• Kind of like a row in Excel, where you give a name to each column.
• Then, you could access the cars’ colors, by car = cars[i]if car.color == “blue”: # do something…
![Page 6: CS104: Chapter 15](https://reader036.fdocuments.net/reader036/viewer/2022082612/56813d2b550346895da6f0f1/html5/thumbnails/6.jpg)
So…
• We need a way to group multiple attributes of something into a single container, and then be able to access, with nice names, the attributes in the container.
• Enter: the class.
![Page 7: CS104: Chapter 15](https://reader036.fdocuments.net/reader036/viewer/2022082612/56813d2b550346895da6f0f1/html5/thumbnails/7.jpg)
Terminology
• A class defines the template or recipe for what an object looks like – i.e., what attributes it has.
• Defining a class does not make any variables (called objects). It just defines what an object would hold if you did create one.
• You instantiate a class to make an object, aka a variable. An object is also called an instance of a class.
![Page 8: CS104: Chapter 15](https://reader036.fdocuments.net/reader036/viewer/2022082612/56813d2b550346895da6f0f1/html5/thumbnails/8.jpg)
Example: Car classclass Car: # “class” is like “def”, but for
# classes, not functions. def __init__(self): “””Constructor for a Car.””” self._make = “” self._model = “” self._yr = 1900 self._color = “”
# main code: instantiate some car objectscar1 = Car() # call __init__, the constructor.car1._make = “Honda”car1._model = “Odyssey” # etccar1._yr = 2001car1._color = “silver”
![Page 9: CS104: Chapter 15](https://reader036.fdocuments.net/reader036/viewer/2022082612/56813d2b550346895da6f0f1/html5/thumbnails/9.jpg)
Using a car instance
• Now that we’ve wrapped up multiple attributes into one container (i.e., “object”), and given each a nice readable name, we can deal with each container as one thing.– get/set values in it– pass it as a parameter– return it from a function– sort the objects, without mixing up whose values
go with whose…
![Page 10: CS104: Chapter 15](https://reader036.fdocuments.net/reader036/viewer/2022082612/56813d2b550346895da6f0f1/html5/thumbnails/10.jpg)
Technical Details
• If student is an object (of class Student) with attribute, id, you can access it with student.id.
• Similar to module.variable or module.func()
• module != class. Module is a file containing variable, function (and class) definitions. Class is a new type. But both are containers.
![Page 11: CS104: Chapter 15](https://reader036.fdocuments.net/reader036/viewer/2022082612/56813d2b550346895da6f0f1/html5/thumbnails/11.jpg)
Q and As
• Can you talk more about the object diagrams?• Can you walk through the example in which
you are making/defining the class point on page 3 -- specifically where “p is an alias for blank”?
• Is there a limit to the amount of attributes a class can have?
![Page 12: CS104: Chapter 15](https://reader036.fdocuments.net/reader036/viewer/2022082612/56813d2b550346895da6f0f1/html5/thumbnails/12.jpg)
Q and As (2)
• We talk about how objects are mutable, but if we can define the attributes of a created object can we define them to be immutable?
• Is a “class definition” like a “function definition” with just a different kind of pattern? (Does it work similar to a function definition?)
![Page 13: CS104: Chapter 15](https://reader036.fdocuments.net/reader036/viewer/2022082612/56813d2b550346895da6f0f1/html5/thumbnails/13.jpg)
Q and A (3)
• Why doesn't the class “Point” clarify what its attributes are? How do you know what the attributes of a class are?