Changes

Jump to: navigation, search

OPS435 Python Lab 6

81 bytes removed, 12:29, 29 November 2017
PART 2 - Understanding Class Structure
:# This is how you give the class a name, now when you need to create a new Student object we know the class is called Student. '''Everything''' indented underneath the '''class Student:''' will be a part of the class.
:# Next indented under the class is the __init__() method. This 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 method, instead of manually calling this method, 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 class and 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.number = number 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 Name: ' + self.name) 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 methods inside of a class, and allows the 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): self.courses[course] = grade
</source>
:# This final method will do a bit of calculations. It will get the average of all values found inside the dictionary, add them up, and divide them by the number of values. When it finishes, the method will print out a message with the GPA of the student inside.<source lang="python">
# Calculate the grade point average of all courses and display it def displayGPA(self): gpa = 0.0 for course in self.courses.keys(): gpa = gpa + self.courses[course] 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? Before you can use a class, we must create a new object. A single class definition can be used to create as many objects as you like, not just a single object. Our '''Student''' class will be used to create lots of different student objects, each object will be created using the blueprint of the class, but they will all be separate and contain separate data to store each and every student's data. Try to think of an object as any other python data structure (list, set, dictionary): you can create multiple dictionaries and store different data inside, even if some of the data is the same.
</source>
:# The final part to creating objects is understanding how to pass values into newly created objects, such as, '''student2 = Student('Jessica', '023384103')'''. How did it know that Jessica is the name instead of the long string of numbers? This is done when the class is written, inside the __init__() method. Look at the arguments used for in the __init__ definition:<source lang="python">
def __init__(self, name, number): self.name = name self.number = number self.courses = {}
</source>
:# First was self, but this is required and we can ignore this for now. Next is '''name''', this is the classes first argument when we instantiate(create) it, the second argument is '''number'''.<source lang="python">
198
edits

Navigation menu