Using Classes - Introduction to Computing Using Python - Lecture Slides, Slides of Introduction to Computing

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

2012/2013

Uploaded on 08/17/2013

bakul
bakul 🇮🇳

4.6

(16)

69 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Recall: Objects as Data in Folders!
An object is like a manila folder!
It can contain variables!
§Variables are attributes!
§Can change values of an attribute"
(with assignment statements)!
It has a “tab” that identifies it!
§Unique identifier assigned by Python!
§This is fixed for the lifetime of the
object!
Point!
x 2.0
y 3.0
z 5.0
id1!
Unique"
identifier"
on tab!
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Using Classes - Introduction to Computing Using Python - Lecture Slides and more Slides Introduction to Computing in PDF only on Docsity!

Recall: Objects as Data in Folders

• An object is like a manila folder

• It can contain variables

§ Variables are attributes

§ Can change values of an attribute

(with assignment statements)

• It has a “tab” that identifies it

§ Unique identifier assigned by Python

§ This is fixed for the lifetime of the

object

Point

x 2.

y 3.

z 5.

id

Unique

identifier

on tab

Recall: Classes are Types for Objects

• Objects must have types

§ 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
  • RGB
  • Turtle
  • Window

Types

  • int
  • float
  • bool
  • str

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

Machinery vs. use of machinery

• Classes in Python provide some very simple

machinery, and very few constraints on how you

use it.

• Learning to program with classes in Python

means learning two things:

1. how the machinery works (this lecture)

2. some ways to use the machinery effectively (next

lecture)

The Class Definition

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

more on this later

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

Populating a class with methods

class Example2(object):

"""A class that defines some things."""

This is a class variable.

a = 29

This is a method that

writes to an instance variable.

def set_b(self, x):

self.b = x

This is a method that reads

from a class variable and an

instance variable.

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.

Method calls

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

Aside: The value None!

  • The boss field is a problem.

§ 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.

  • Solution : use value None

§ None : Lack of (folder) name

§ Will reassign the field later!

  • Be careful with None variables

§ 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

Evaluating a Constructor Expression

Worker('Obama', 1234, None)

  1. Create a new object (folder) that

is an instance of the class

§ Instance is initially empty

  1. Call the method init "

(if it exists)

§ Pass folder ID to self

§ Pass other arguments in order

  1. Returns the object (folder) name

as final value of expression

Worker

ssn 1234

id

lname Obama

boss None

Making Arguments Optional

  • We can assign default values

to init arguments

§ Write as assignments to

parameters in definition

§ Parameters with default

values are optional

  • Examples :

§ 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.

What does str() do on class objects?

  • Does NOT display contents

>>> p = Point(1,2,3)

>>> str(p)

'’

  • To display contents, you must

implement a special method

called str

  • With the defns. on these slides:

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 + ')')