198
edits
Changes
→INVESTIGATION 1: Creating Classes
= INVESTIGATION 1: Creating Classes =
:In this first investigation you will be introduced to classes. A class is a blue print to creating an object, first we write what we want the object to contain inside the class, then we can instantiate(create the object) by providing the class type. Once an object is created, interacting with the object will look very familiar, because you have been using objects throughout the entire course, this the only difference is these object will be no differentcreated by you.
== PART 1 - Creating a Class ==
:The class that is written in Python can contain variables, functions, code, but non of the code is executed or run until you use the class is used to create an the object. Remember that the class is a blueprint for how your object will work, the object that you create will have be created is what will actually be running the code running inside it. First lets create This of a classin the same way as a function definition, the function doesn't run until it's executed, code inside classes don't run until the are made into objects.
:'''Perform the Following Steps:'''
print('GPA of student ' + self.name + ' is ' + str(gpa / len(self.courses)))
</source>
:# This is the Student class, with this class multiple Student objects can be created. There are a number of parts of this script that should look familiar, such as functions, for loops, variables. Functions indented underneath the class definition are called methods. Variables starting with '''defself.''' for functions, for loops, variablesare called public attributes. In the next steps lets break down this class and explain each part in detail.
== PART 2 - Understanding Class Structure ==
</source>
:# This is how you give the class a name, now when you need to create a new Student object we know it's calledd the class is called Student. '''Everything''' indented underneath the '''class Student:''' will be apart of the class.:# Next indented under the class is the __init__() method. This work works similarly to a function, it contains code indented underneath it, that code is executed when you call the function. But __init__() is a special functionmethod, instead of manually calling this functionmethod, it will automatically be executed when we create a object. Inside the __init__() we create variables(object attributes), these are created using '''self.name''', where '''name''' is the name of the attribute. The '''self.''' portion before the variable name is to let the class know that this variable can be accessed from anywhere inside the classand outside of the class, as a public attribute. Lets come back to this later.<source lang="python">
def __init__(self, name, number):
self.name = name
self.courses = {}
</source>
:# The next method definition prints out variables self.name and self.number, both of these variables were set in the __init__() method. Normally creating variables inside a function means they can only be accessed inside of that specific function. But classes give us the ability to share data throughout the class with all other objects. Place '''self.''' before the variable name to allow that variable to be shared with different functions, these variables are called public attributes. However, as we will soon come to see this variable is only shared within a single instance of the class(more on this to come). <source lang="python">
# Display student name and number
def displayStudent(self):
print('Student Number: ' + self.number)
</source>
:# This next method accepts some arguments. The first argument of the method is '''self''', this is a required syntax for making functions methods inside of a class, and allows the function method to access any variables you created/saved in the class previously, such as, '''self.courses'''. This method will take two additional arguments, course, and grade. The method will then store these inside a dictionary '''self.courses''', with the key being '''course''' and the value being '''grade'''.<source lang="python">
# Add a new course and grade to students record
def addGrade(self, course, grade):
print('GPA of student ' + self.name + ' is ' + str(gpa / len(self.courses)))
</source>
:# Now that the class has been broken down, the code inside is simple on it's own, but what does it mean when it all works together. So before you can use a class, we must create a new object. A single class can create many objects, not just a single object. This class will be used to create lots of different student objects, each object will be created using the blue print of the class, but they will all be separate and contain separate data to store each and every students data. Just like Try to think of an object as any other python data structure(list, set, dictionary), you can create multiple dictionaries and store difference data inside, even if some of the student beside you are both students, doesn't mean you are data is the same student.
:# Import the python script into your ipython environment:<source lang="python">
from student import Student
=== Create a Python Script Demonstrating Classes ===
:# The following python script is broken. It has two major problemsto fix and one new feature to add: first problem is providing the student number as an integer causes an error(TypeError) when displayStudent() is run, second problem is in displayGPA() may divide by zero erorr(ZeroDivisionError) if no courses are added to the dictionary or the grades added to the dictionary are 0.0 floats. Finally, you will add a new method to this class that prints out a formatted list of all courses the student has taken.
:#Create the '''~/ops435/lab6/lab6a.py''' script.
:#Use the following as a template(warning this is NOT the same as student.py):<source lang="python">