Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CP164: Lab 01

Classes: Object oriented way of thinking bundling data and functionality together
● Creates a new type of object allowing new instances if that class to be made
Ex. Think of movies as a class and “the new instances” as the “GENRES” within the class
Creating Objects:
Two parts are required to create a class:
(1) The name of the class
(2) The class constructor which is always named “__init__”
Example of creating a class:
class Name: (1)(Name of class)
Def __init__(self[,param,]): (2)(Class Constructor)
Note: The constructor must always start with the keyword “def”. All classes must contain at least
1 parameter.
● Class methods MUST be indented within the class to show that they belong to the class
● You can call a class by using the name of the class not the name of the constructor

Ex. Object = Name([argument,])

Importing a class:
● We can import a class by using the following syntax “from module import class”
Note: The keyword “self” is not used as an argument, but is used in order to create the object
method
Class Attributes:
● Class attributes are defined within the class constructor (self is not a class attribute) by
putting the keyword “self” in front of the class attribute

Ex.
Class Number: (1) Name of class
Def __init__(self, value): (class attribute named “value”)
return
This attribution class named “value” can be now used into another Pyhton module and used to
define object:
Ex.
From Number import Number
Example = Number(0)

Using Methods:

Class Methods Require:


(1) The keyword self as the first (or only) parameter
(2) Class attributes by preceding them with “self”
(3) Class methods are called by preceding them with the name of an object
Ex.
def square(self):
result = self.value * self.value
return result
Then calls it from the example object:
answer = example.square()
Accessing Attributes: Objects attributes can be accessed by using the syntax: object.attribute
● You can use this method to access or change an attribute
Ex.
print(example.value)
example.value = 0
example.value = int(input(“Enter a new value))
answer = example.value * 2
Note: Attributes and methods with “-” or “__” should not be accessed from outside the
class definition
● Considered private attributes and methods accessible only by other class methods

● Python doesn’t have a way to make attributes and methods truly private or
inaccessible from outside the class
Print Lists of Objects:

Note: when printing a lists of objects, you must make sure you print each object separately
Ex.
fv = open(“Students.txt”,”r”)
Students = read_students(fv)
fv.close()

for student in students:


print(student)
print()
Things to keep in mind when using project references:
● All references must be open when attempting to run a program
● Submit all referenced projects as part of your Eclipse archive file
● Do not use the same PyDev module names in two different referenced projects or Eclipse
will not know which one an import statement is referring to.
● Project references go one way only. If the project lab02_stacks references the project
osaw2891_data_structures , then osaw2891_data_structures should not reference
lab02_stacks. Circular references are a very bad idea.
● Upon occasion referenced projects may become 'out of sync' with the project that
references them. To fix this, right-click on the project and choose Refresh to update
Eclipse from the file system.
● Non-PyDev modules cannot be referenced from another project, so to read file such as
movies.txt or food.txt you have to copy it into every project you need to read it.

You might also like