17.4 User Defined Classes¶
- We’ve already seen classes like
strintfloatandturtle - In many cases when we are solving problems we need to create data objects that are related to the problem we are trying to solve. We need to create our own classes.
Walk-through Example¶
-
pointobject
-
Some of the typical operations that one associates with points might be to ask the point for its x coordinate,
getX, or to ask for its y coordinate,getY
-
Now that we understand what a
pointobject might look like, we can define a new class. - We’ll want our points to each have an
xand ayattribute, so our first class definition looks like this:
class Point:
""" Point class for representing and manipulating x,y coordinates. """
def __init__(self):
""" Create a new point at the origin """
self.x = 0
self.y = 0
- Class definitions can appear anywhere in a program, but they are usually near the beginning (after the
importstatements) - The syntax rules for a class definition are the same as for other compound statements.
- Header which begins with the keyword,
class, followed by the name of the class, and ending with a colon.
- Header which begins with the keyword,
- If the first line after the class header is a string, it becomes the docstring of the class, and will be recognized by various tools. DON'T FORGET DOCSTRINGS
- This is also the way docstrings work in functions.
- Every class should have a method with the special name
__init__- This initializer method, often referred to as the constructor, is automatically called whenever a new instance of our
Pointobject is created. - It gives the programmer the opportunity to set up the attributes required within the new instance by giving them their initial state values
- The
selfparameter (you could choose any other name, but nobody ever does!) is automatically set to reference the newly-created object that needs to be initialized.
- The
- This initializer method, often referred to as the constructor, is automatically called whenever a new instance of our
class Point:
""" Point class for representing and manipulating x,y coordinates. """
def __init__(self):
""" Create a new point at the origin """
self.x = 0
self.y = 0
p = Point() # Instantiate an object of type Point
q = Point() # and make a second point
- During the initialization of the objects, we created two attributes called x and y for each, and gave them both the value 0.
-
Two
Pointshave been created, each having an x and y coordinate with value 0.
-
Note
- The assignments are not to
xandy, but toself.xandself.y. The attributesxandyare always attached to a particular instance. The instance is always explicitly referenced with dot notation.
- The assignments are not to
- The variables
pandqare assigned references to two newPointobjects - A function like
TurtleorPointthat creates a new object instance is called a constructor - Every class automatically uses the name of the class as the name of the constructor function.
- The definition of the constructor function is done when you write the
__init__function.
Metaphor:¶
- The class itself isn’t an instance of a point, but it contains the machinery to make point instances
- Every time you call the constructor, you’re asking the factory to make you a new object
- As the object comes off the production line, its initialization method is executed to get the object properly set up with its factory default settings
- The combined process of “make me a new object” and “get its settings initialized to the factory default settings” is called instantiation.