lecture13

lecture13 - CS 1: Fall 2010 Lecture 13 : November 8, 2010...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 1: Fall 2010 Lecture 13 : November 8, 2010 More classes Caltech CS 1: Fall 2010 Classes and object-oriented programming, part 1 The class statement creating your own objects Constructors Defining new methods Caltech CS 1: Fall 2010 We'll use classes to develop a realistic example Caltech CS 1: Fall 2010 An object is a data value that contains attributes (data items) methods (functions that act on the object) A class is a description of what an object is contains the definition of all methods for an object Every object is an instance of some class Caltech CS 1: Fall 2010 Every object of the same class contains the exact same methods Every object of the same class contains the same attribute names , but the values for these attributes are generally different We can think of an object as a "bag of attributes with methods" N.B. attributes of objects are often called fields instead (shorter name for same thing) Caltech CS 1: Fall 2010 Every class has a constructor method (or constructor for short) called __init__ The constructor is called when an object is created The attributes of an object are normally initialized in the constructor though the values can be changed later Caltech CS 1: Fall 2010 Method definitions look like regular Python function definitions except they are located inside a class statement Method definitions have a first argument called self which represents the object being acted on Inside a method, can access the attributes of the current object through the self argument Caltech CS 1: Fall 2010 When methods are called, the method call is missing the first ( self ) argument that argument goes before the dot in the dot syntax So if you have a class Thingy with an instance t and a method called getValue : t.getValue() is the same as: Thingy.getValue(t) # t self Caltech CS 1: Fall 2010 There's more to know about classes and objects, but that's enough for now Let's recap the graphics example we discussed last time and extend it Caltech CS 1: Fall 2010 We want to create objects to represent things we can draw on canvases squares, circles We'll start with a square object Caltech CS 1: Fall 2010 class Square: '''Objects of this class represent a square on a Tkinter canvas.''' def __init__(self, canvas, center, size, color): ... <other methods> Caltech CS 1: Fall 2010 def __init__ (self, canvas, center, size, color): (x, y) = center x1 = x size/2 # upper left y1 = y size/2 # coordinates x2 = x + size/2 # lower right y2 = y + size/2 # coordinates self.handle = \ canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline=color) Caltech CS 1: Fall 2010 Creating squares: s1 = Square(canvas, (100, 100), 50, 'red') s2 = Square(canvas, (230, 110), 100, 'blue') s3 = Square(canvas, (50, 240), 75, 'green') This puts three squares on a canvas Each square knows what its handle is However, we need to add some more attributes to the object Caltech CS 1: Fall 2010...
View Full Document

This note was uploaded on 02/22/2011 for the course CS 1 taught by Professor Pinkston,d during the Fall '08 term at Caltech.

Page1 / 56

lecture13 - CS 1: Fall 2010 Lecture 13 : November 8, 2010...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online