Class Notes (1,000,000)
CA (620,000)
UTSG (50,000)
CSC (1,000)
CSC148H1 (100)
Lecture 1

CSC148H1 Lecture Notes - Lecture 1: Docstring, Init

Computer Science
Course Code
Joseph Lim

This preview shows half of the first page. to view the full 2 pages of the document.
CSC148- Lecture 1- Introduction to Computer Science
unique instance of a data structure modelling a real world concept
everything in Python is an object: str, int, list, dict, etc.
Defining a Class API:
1. Choose a class name and write a brief description in the class docstring
2. Write some examples of client code that uses your class
3. Decide what services your class should provide as public methods, for each method
declare an API (examples, header, type contract, description)
4. Decide which attributes you class should provide without calling a method, list them in
the class docstring
Class Example in Python:
>>> Class Rectangle:
def __init__(self, width, height):
print (“I am initializing”)
self.width = width
self.height = height
def CalculateArea(self)
return self.width * self.height
>>> rectangle1 = Rectangle(3,6)
I am initializing
>>> print (rectangle1.CalculateArea())
Class Point Example:
>>> Class Point:
def __init__(self, x, y)
self.x = float(x)
self.y = float(y)
def __eq__(self, other):
Return the point if the points are the same.
return (type(self) == type(other) and self.x == other.x and self.y == other.y)
You're Reading a Preview

Unlock to view full version