









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Key points in this Introduction to Computing Using Python lecture are: Using Classes Effectively, Classpoint, Designing Types, Type, Making Class a Type, Planning Out a Class, Class Invariant, Method Specification, Initializer . Objectives of this course are: 1.Fluency in (Python) procedural programming 2. Competency in object-oriented programming 3. Knowledge of searching and sorting algorithms
Typology: Slides
1 / 15
This page cannot be seen from the preview
Don't miss anything!










Point
x 2.
y 3.
z 5.
id
Unique
identifier
on tab
§ Some types are built in (float, int, file, list, …)
§ Other types are defined by classes
§ Classes are how we add new types
to Python
Classes
Point
x 2.
y 3.
z 5.
id
id
0 1
2
id
3
a list: contains
indexed items
a class object:
contains only
named attributes
class name
class < class-name >(object):
"""Class specification"""
< function definitions >
< assignment statements >
< any other statements also allowed>
Goes inside a
module, just
like a function
definition.
keyword class
indicates a
class definition
docstring, just
like a function
definition
don’t forget the colon!
to define
methods
…but not often used
to define
variables
class Example(object):
"""The simplest possible class."""
pass
Example
class Example2(object):
"""A class that defines some things."""
a = 29
def set_b(self, x):
self.b = x
def f(self):
return self.a * self.b
a
set_b()
f()
Example
Everything
defined in the
class definition
creates attributes
of the class.
A variable that lives in a class is
a class variable.
A function that lives in a
class defines a method.
Every method has a
special first parameter
self that receives a
reference to the
instance the method
was called on.
This assignment will create
an instance variable.
Given class definition from previous slide:
e = Example2()
§ constructor expression assigned to e
§ creates a new instance, stores ID in e
e.set_b(42)
§ method call has object + one argument
§ turns into function call with 2 arguments
§ value of e passed to self; 42 passed to x
§ assignment to self.b creates instance var.
print e.f()
§ method call has object + no arguments
§ turns into function call with 1 arguments
§ value of e passed to self
§ attribute references find self.a in class,
self.b in instance
self x
set_b: 1
id2 42
a
set_b()
f()
Example
Example
b 42
id
id e
self
f: 1
id
§ boss is supposed to refer to
a Worker object
§ But some workers might not
have a boss
§ Maybe not assigned yet,
maybe the buck stops there.
§ None : Lack of (folder) name
§ Will reassign the field later!
§ var3.x gives error!
§ There is no name in var
§ Which Point to use?
id
var
id
var
None
var
Point
x 2.
y 5.
z 6.
id
Point
x 3.
y –2.
z 0.
id
Worker('Obama', 1234, None)
is an instance of the class
§ Instance is initially empty
(if it exists)
§ Pass folder ID to self
§ Pass other arguments in order
as final value of expression
Worker
ssn 1234
id
lname Obama
boss None
to init arguments
§ Write as assignments to
parameters in definition
§ Parameters with default
values are optional
§ p = Point() # (0,0,0)
§ p = Point(1,2,3) # (1,2,3)
§ p = Point(1,2) # (1,2,0)
§ p = Point(y=3) # (0,3,0)
§ p = Point(1,z=2) # (1,0,2)
class Point(object):
"""Instances are points in 3d space
x [float]: x coord
y [float]: y coord
z [float]: z coord"""
def init(self, x=0, y=0, z=0):
self.x = float(x)
self.y = float(y)
self.z = float(z)
…
Assigns in order
Use parameter name
when out of order
Can mix two
approaches
Not limited to methods.
Can do with any function.
>>> p = Point(1,2,3)
>>> str(p)
'’
implement a special method
called str
print Point(3,4,5)
produces the output:
class Point(object):
"""Instances are points in 3d space""”
…
def str(self):
"""Returns: string with contents"""
return ('(' + self.x + ',' +
self.y + ',' +
self.z + ')')