Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1."— Presentation transcript:

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

2 Lesson objectives 1. Write a class definition 2. Instantiate objects 3. Assign values to attributes 4. Change attribute values 5. Copy objects 6/16/09 Python Mini-Course: Lesson 26 2

3 Class definition Use the class keyword Example: class Point(object): """ represents a point in 2-D space """ print Point 6/16/09 Python Mini-Course: Lesson 26 3

4 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/09 Python Mini-Course: Lesson 26 4

5 Assigning attribute values Use the object name and the attribute name with dot notation Example: p1.x = 3.0 p1.y = 4.0 print p1.x, p1.y 6/16/09 Python Mini-Course: Lesson 26 5

6 Assigning attribute values These are called instance attributes p2 = Point() p2.x, p2.y = 12.0, 13.0 print p1.x, p1.y print p2.x, p2.y 6/16/09 Python Mini-Course: Lesson 26 6

7 Note on attributes Python handles attributes somewhat differently from other OOP languages All attributes are "public" Attributes can be created on the fly 6/16/09 Python Mini-Course: Lesson 26 7

8 Using attributes Object attributes are just like any other variable print '(%g, %g)' % (p1.x, p1.y) from math import sqrt distance = sqrt(p1.x**2 + p2.y**2) print distance 6/16/09 Python Mini-Course: Lesson 26 8

9 Note: Objects are data types You can pass objects to functions and return them from functions, just like any other data type 6/16/09 Python Mini-Course: Lesson 26 9

10 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/09 Python Mini-Course: Lesson 26 10

11 Encapsulation Examples rectangle1.py rectangle2.py rectangle3.py 6/16/09 Python Mini-Course: Lesson 26 11

12 Aliasing Try this: box = Rectangle(10.0, 200.0) box1 = box box1.width = 100.0 print box.width, box1.width box1 is box 6/16/09 Python Mini-Course: Lesson 26 12

13 Copying Use the copy module import copy box2 = copy.copy(box) box2.width = 50.0 print box.width, box2.width box2 is box 6/16/09 Python Mini-Course: Lesson 26 13

14 Limitations of copying box2.corner.x = 2.0 print box.corner.x box2.corner is box.corner 6/16/09 Python Mini-Course: Lesson 26 14

15 Deep copying Use the copy module box3 = copy.deepcopy(box) box3.corner.x = 1.0 print box.corner.x, box3.corner.x box3.corner is box.corner 6/16/09 Python Mini-Course: Lesson 26 15


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

Similar presentations


Ads by Google