Using Classes Effectively - 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, Objects, Data in Folder, Attributes, Types, Classes Are Types for Objects, Objects Can Have Methods, Machinery, Class Definition, Method Calls, Initializing Instances, Constructor Expression . 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 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Important!!
YES!
class Point(object):!
"""Instances are 3D points!
x [float]: x coord!
y [float]: y coord!
z [float]: z coord"""!
!
!
NO!
class Point:!
"""Instances are 3D points!
x [float]: x coord!
y [float]: y coord!
z [float]: z coord"""!
!
!
“Classic” Classes!
No reason to use these!
3.0-Style Classes!
Well-designed!
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

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

Important!

YES

class Point(object):

"""Instances are 3D points

x [float]: x coord

y [float]: y coord

z [float]: z coord"""

NO

class Point:

"""Instances are 3D points

x [float]: x coord

y [float]: y coord

z [float]: z coord"""

“Classic” Classes

No reason to use these

3.0-Style Classes

Well-designed

Designing types

• One definition of a type : a set of objects with the

operations on those objects.

§ int—set: integers; ops: +, –, *, /, …

§ Time—set: times of day; ops: time span, before/after, …

§ Worker—set: all possible workers; ops: hire, pay, promote, …

§ Rectangle—set: all axis-aligned rectangles in 2D; ops:

contains, intersect, …

• When you define a class, you are (should be) thinking

of a “real type” you want to create

§ Python gives you the tools to do this, but doesn’t do it for you

§ Physically, any object can take on any value

§ Discipline is required to get what you want

Planning out a class

class Time(object): """Instances represent times of day. Instance variables: hour [int]: hour of day, in 0..23" min [int]: minute of hour, in 0.. """ def init(self, hour, min): """The time hour:min." Pre: hour in 0..23; min in 0..59""" def increment(self, hours, mins): """Move this time hours and minutes into the future." Pre: hours [int] >= 0; mins in 0..59""" def is_pm(self): """Returns: this time is noon or " later.""" class invariant States what attributes are present and what values they can have. A statement that will always be true of Time instances. method specification States what the method does. Gives preconditions stating what is assumed to be true of the arguments.

Planning out a class

class Rectangle(object): """Instances represent rectangular" regions of the plane. Instance variables: t [float]: y coordinate of top edge" l [float]: x coordinate of left edge" b [float]: y coordinate of bottom edge" r [float]: x coordinate of right edge For all Rectangles, l <= r and b <= t. """ def init(self, t, l, b, r): """The rectangle [l, r] x [t, b] Pre: args are floats; l <= r; b <= t""" def area(self): """Return: area of the rectangle.""" def intersection(self, other): """Return: new Rectangle describing intersection of self with other.""" class invariant States what attributes are present and what values they can have. A statement that will always be true of Rectangle instances. method specification States what the method does. Gives preconditions stating what is assumed to be true of the arguments.

Implementing a class

• All that remains is to fill in the methods. (All?!)

• When implementing methods:

§ Assume preconditions are true

§ Assume class invariant is true to start

§ Ensure method specification is fulfilled

§ Ensure class invariant is true when done

• Later, when using the class:

§ When calling methods, ensure preconditions are true

§ If attributes are altered, ensure class invariant is true

Instance variables: hour [int]: hour of day, in 0..23" min [int]: minute of hour, in 0..

Implementing an initializer

def init(self, hour, min): """The time hour:min." Pre: hour in 0..23; min in 0..59"""

You put code here

This is true to start

This should be true

at the end

self.hour = hour self.min = min

The view from outside

  • Invariants and preconditions

serve two purposes

  • They are tools for you, as the

author, to think through your

plans in a disciplined way

  • They communicate to the user*

of the class how they are

allowed to use it

  • Together they are the interface

of the class

§ interface between two programmers § interface between two parts of the program

  • …who might well be you! in•ter•face |ˈintərˌfās| noun
  1. a point where two systems, subjects, organizations, etc., meet and interact : the interface between accountancy and the law.
    • chiefly Physics a surface forming a common boundary between two portions of matter or space, e.g., between two immiscible liquids : the surface tension of a liquid at its air/ liquid interface.
  2. Computing a device or program enabling a user to communicate with a computer.
    • a device or program for connecting two items of hardware or software so that they can be operated jointly or communicate with each other. —The Oxford American Dictionary