Docsity

Understanding Objects in Python: Points, Attributes, and Methods, Slides of Introduction to Computing

Partial preview of the text

Download Understanding Objects in Python: Points, Attributes, and Methods and more Slides Introduction to Computing in PDF only on Docsity!

Example: Points in 3D space

  • Want a point in 3D space § We need three variables § x , y , z coordinates
  • What if we have many points? § Vars x0, y0, z0 for first point § Vars x1, y1, z1 for next point § … § This can get really messy
  • How about a single variable

that represents a point?

x 2. y 3. z 5.

Example: Points in 3D space

  • Want a point in 3D space § We need three variables § x , y , z coordinates
  • What if we have many points? § Vars x0, y0, z0 for first point § Vars x1, y1, z1 for next point § … § This can get really messy
  • How about a single variable

that represents a point?

x 2. y 3. z 5.

Point

Classes: Types for Objects

• Everything needs a type

§ An object’s type is a class

• Modules provide classes

§ Example : point.py

§ Import to use Point

• We’ll learn how to define

classes later

§ Do not try to understand the

contents of point.py

§ Lots more to learn first

class name x 2. y 3. z 5. id

Constructor: Function to Make Objects

• How do we create objects?

§ Other types have literals § Example : 1 , "abc", True

• Constructor Function :

§ Same name as the class § Example: Point(0, 0, 0) § Makes an object (manila folder) § Returns folder ID as its value

• Example: p = Point(0, 0, 0)

§ Creates a Point object § Stores object’s ID in p p id Variable stores ID not object instantiated object Point x 0. y 0. z 0. id

Objects and Attributes

• Attributes are variables 

that live in objects

§ Can use in expressions § Can assign values to them

• Access: ⟨ variable ⟩.⟨ attribute ⟩

§ Example: p.x § Same syntax as accessing a variable in a module: math.pi

• Putting it all together

p = Point(1, 2, 3)

p.x = p.y + p.z

p id Point x 1. y 2. z 3. id

Exercise: Attribute Assignment

• Create point; name into q and p

p = Point(0,0,0) q = p

• Execute the assignments:

p.x = 5. q.x = 7.

• What is value of p.x?

p q A: 5. B: 7. C: id D: I don’t know Point x 0. y 0. z 0. id id id

Methods: Functions Tied to Objects

• Method : function tied to object

§ Method call looks like a function call preceded by a variable name: ⟨ variable ⟩.⟨ method ⟩(⟨ arguments ⟩) § Example: p.distanceFromOrigin() § Example: p.distanceTo(q)

• Name resolution

§ ⟨ object ⟩.⟨ name ⟩ means “go to object and look for something called name .” § Python looks first in the object’s folder, then in the object’s class Point x 5. y 2. z 3. id init(x, y, z) distanceFromOrigin() distanceTo(other) Point p id

Surprise: All Values are in Objects!

• Including basic values

§ int, float, bool, str

• Example :

x = 2. id(x)

• But they are special

§ They are immutable  (contents cannot change) § Distinction between value and identity is immaterial § So we can ignore the folder (^) x 2. float

id x id

Strings Have Methods Too

• We have seen expressions

like s.index('a')

• Now we can recognize

them as method calls

• String methods do not

change the string

§ Can’t: strings immutable § “Modifications” made by returning a new string § s.replace('o','uh') evaluates to 'Helluh Wuhld!' but s is still 'Hello World' x “foo” str “foo” id x id index(substring) replace(old, new) … str

Class Objects are Mutable

• Unlike int, str, etc., objects

of class type (and some

others) are mutable

§ You can change them § Methods can have effects besides their return value

• Example:

f = open('jabber.txt')

s = f.read()

f.close()

• Example: p.projectToFloor()

file id f id name, position, state, … close() read([size]) readline(), … file Opens a file on your hard disk, returns a file object you can read from http://docs.python.org/2/library/stdtypes.html#file-objects