Changes

Jump to: navigation, search

OPS435 Python Lab 6

408 bytes removed, 00:14, 26 February 2018
PART 2 - Understanding Class Structure
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):
</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 description 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.
:# Import In a new file, import the python script into your ipython environmentStudent class:<source lang="python">
from student import Student
</source>
student1 = Student('John', '013454900')
</source>
:# Now that the object '''student1''' has been created, Have a look closely at the components inside contents of the object. This is a look into the objects '''namespace'''student1:<source lang="python">dirprint(student1.name)</source>:# Notice anything familiar in here? This will display all attributes(name, number, courses) that were created with '''self.variableName''', it also shows all the methodsprint(displayStudent, displayGPA, addCourse) that were created too. Take a closer look at some of these different attributes and methods.<source lang="python">student1.namestudent1.numberprint(student1.courses
student1.displayStudent()
</source>
</source>
:# Take a closer look at some of these different attributes and methods.<source lang="python">
print(student2.name)print(student2.numberprint(student2.courses
student2.displayStudent()
</source>
</source>
:# Investigate what has changed in each object:<source lang="python">
print(student1.name)print(student1.courses)print(student2.name)print(student2.courses)
</source>
:# The method '''addGrade()''' changes the '''self.courses''' dictionary. But both student1 and student2 have their OWN courses dictionary.
:# Once an object is created the attributes inside may be modified externally. Though you may need to be careful, if a value is a string, and you change it to another type such as a integer or a list, it's possible the class was not designed to deal with the different type and may throw a error when you run a method. For example, changing the name to a integer would break the displayStudent method, because it would try and concatenate strings and integers(maybe the method should be written better.<source lang="python">
# student1.name is a string like any other
print(student1.name)
student1.name = 'Jack'
student1.name
print(student1.name)
len(student1.name)

Navigation menu