{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture13 - Lecture 13 November 8 2010 More classes Caltech...

Info icon This preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 Lecture 13 : November 8, 2010 More classes
Image of page 1

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

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 Classes and object-oriented programming, part 1 The class statement creating your own objects Constructors Defining new methods
Image of page 2
Caltech CS 1: Fall 2010 We'll use classes to develop a realistic example
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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)
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
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
Image of page 7

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

View Full Document Right Arrow Icon
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
Image of page 8
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
Image of page 9

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

View Full Document Right Arrow Icon
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
Image of page 10
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>
Image of page 11

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

View Full Document Right Arrow Icon
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)
Image of page 12
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
Image of page 13

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

View Full Document Right Arrow Icon
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
Image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern