Top Banner
Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1
15

Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.

Dec 14, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.

Python Mini-CourseUniversity of Oklahoma

Department of Psychology

Lesson 26Classes and Objects

6/16/09Python Mini-Course: Lesson 261

Page 2: Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.

Lesson objectives

1. Write a class definition2. Instantiate objects3. Assign values to attributes4. Change attribute values5. Copy objects

6/16/09Python Mini-Course: Lesson 262

Page 3: Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.

Class definition

Use the class keywordExample:class Point(object):

"""represents a point in 2-D space"""

print Point

6/16/09Python Mini-Course: Lesson 263

Page 4: Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.

Instantiating an object

Call the class constructor by using the class name as if it were a function

Example:

p1 = Point()

print p1

6/16/09Python Mini-Course: Lesson 264

Page 5: Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.

Assigning attribute values

Use the object name and the attribute name with dot notation

Example:p1.x = 3.0p1.y = 4.0print p1.x, p1.y

6/16/09Python Mini-Course: Lesson 265

Page 6: Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.

Assigning attribute values

These are called instance attributesp2 = Point()p2.x, p2.y = 12.0, 13.0print p1.x, p1.yprint p2.x, p2.y

6/16/09Python Mini-Course: Lesson 266

Page 7: Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.

Note on attributes

Python handles attributes somewhat differently from other OOP languagesAll attributes are "public"Attributes can be created on the fly

6/16/09Python Mini-Course: Lesson 267

Page 8: Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.

Using attributes

Object attributes are just like any other variable

print '(%g, %g)' % (p1.x, p1.y)

from math import sqrtdistance = sqrt(p1.x**2 + p2.y**2)print distance

6/16/09Python Mini-Course: Lesson 268

Page 9: Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.

Note:

Objects are data typesYou can pass objects to functions and return them from functions, just like any other data type

6/16/09Python Mini-Course: Lesson 269

Page 10: Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.

Example:

def print_point(p): print '(%g, %g)' % (p.x, p.y)

print_point(p1)

NB: this example violates the principle of encapsulation

6/16/09Python Mini-Course: Lesson 2610

Page 11: Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.

Encapsulation

Examplesrectangle1.pyrectangle2.pyrectangle3.py

6/16/09Python Mini-Course: Lesson 2611

Page 12: Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.

Aliasing

Try this:box = Rectangle(10.0, 200.0)box1 = boxbox1.width = 100.0print box.width, box1.widthbox1 is box

6/16/09Python Mini-Course: Lesson 2612

Page 13: Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.

Copying

Use the copy moduleimport copybox2 = copy.copy(box)box2.width = 50.0print box.width, box2.widthbox2 is box

6/16/09Python Mini-Course: Lesson 2613

Page 14: Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.

Limitations of copying

box2.corner.x = 2.0print box.corner.xbox2.corner is box.corner

6/16/09Python Mini-Course: Lesson 2614

Page 15: Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.

Deep copying

Use the copy modulebox3 = copy.deepcopy(box)box3.corner.x = 1.0print box.corner.x, box3.corner.x

box3.corner is box.corner

6/16/09Python Mini-Course: Lesson 2615