Docsity

Python Classes and Objects, Summaries of Design

Partial preview of the text

Download Python Classes and Objects and more Summaries Design in PDF only on Docsity!

Python Classes and

Objects

A Basic Introduction Coming up: Topics 1

Topics

  • Objects and Classes
  • Abstraction
  • Encapsulation
  • Messages

What are Objects

  • A Circle drawn on the screen:
  • Has attributes (knows stuff):
    • radius, center, color
  • Has methods (can do stuff):
    • move
    • change color

Design of Circle object

  • A Circle object:
    • center, which remembers the center point of the circle,
    • radius, which stores the length of the circle’s radius.
    • color, which stores the color
  • The draw method examines the center and radius to decide which pixels in a window should be colored.
  • The move method sets the center to another location, and redraws the circle

Circle: classes and objects

Circle Class Attributes:

  • location, radius,color Methods:
  • draw, move Classes are blueprints or directions on how to create an object Objects are instantiations of the class (attributes are set) 3 circle objects are shown (each has different attribute values)

Circle Class

class Circle(object): def init(self, center, radius): self.center = center self.radius = radius def draw(self, canvas): rad = self.radius x1 = self.center[0]-rad y1 = self.center[1]-rad x2 = self.center[0]+rad y2 = self.center[1]+rad canvas.create_oval(x1, y1, x2, y2, fill='green') def move(self, x, y): self.center = [x, y] Beginning of the class definition The constructor. This is called when someone creates a new Circle, these assignments create attributes. A method that uses attributes to draw the circle A method that sets the center to a new location and then redraws it objects/CircleModule.py

Creating a Circle

myCircle = Circle([10,30], 20)

  • This statement creates a new Circle object and stores a reference to it in the variable myCircle.
  • The parameters to the constructor are used to initialize some of the instance variables (center and radius) inside myCircle.

Creating a Circle

myCircle = Circle([10,30], 20)

  • Once the object has been created, it can be manipulated by calling on its methods: myCircle.draw(canvas) myCircle.move(x,y)

Using the Circle

  • from CircleModule import * myCircle = Circle([10,30], 20) print "CENTER :"+str(myCircle.center)

CENTER :(10, 30) To get an instance variable from an object, use: <>.variable What happens if the instance variable doesn’t exist?

Using Instance Variables

myCircle = Circle([10,30], 20) print "CENTER :"+str(circle.carl)

AttributeError: Circle instance has no attribute ’carl’

Summary: Using instance variables

  • Creating new instance variables just means assigning them a value: - self.bob = 234 # In constructor
  • Using instance variables is done through dot notation: - val = myCircle.bob # Outside the class definition - val = self.bob # Inside class methods

Attributes / Instance Variables

  • Attributes represent the characteristics of a class. When an object is instantiated and the values are assigned to attributes, they are then referred to as instance variables.
  • The values of the instance variables define the state of the individual object
  • They are referred to as instance variables because the values assigned to an individual object (instance of a class) are unique to that particular class
  • Attributes may be public or private (although due to their specific implementation, they are not truly private in Python)
  • If the attributes are private, they serve to enforce the concept of information hiding

Messages

  • Process by which system components interact: - send data to another object - request data from another object - request object to perrform some behavior
  • Implemented as methods (not called functions). - Functions are procsses that are object independet - Methods are dependent on the state of the object

Message Passing

  • When calling a method in another class, OO uses the term “message passing” you are passing messages from one class to another
  • Don’t be confused… this is really just a new name for calling a method or a function