Presentation on theme: "CS104: Chapter 15 CS 104 Students and me. Big Question Q: You say creating a class is how to create a new type. Why would we even want to do this?! A:"— Presentation transcript:
CS104: Chapter 15 CS 104 Students and me
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.)
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”, “VIN X95”), …]
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 == “blue”: # do something… Or, was it car? car? 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?
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…
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.
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.
Example: Car class class 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 objects car1 = Car() # call __init__, the constructor. car1._make = “Honda” car1._model = “Odyssey” # etc car1._yr = 2001 car1._color = “silver”
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…
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.
Using objects You can pass to functions Return from functions Use in computations, etc.
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?
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?)
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?