Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Writing the suite of statements in a class


The suite of statements inside a class statement is generally a collection of method definitions. Each method is a function that's bound to the class. The suite of statements can also include assignment statements; these will create variables that are part of the class definition as a whole.

Here's a simple class for an (x, y) coordinate pair:

class Point:
    """
    Point on a plane.
    """
    def __init__(self, x, y):
        self.x= x
        self.y= y
    def __repr__(self):
        return "{cls}({x:.0f}, {y:.0f})".format(
            cls=self.__class__.__name__, x=self.x, y=self.y)

We've provided a class name, Point. We haven't explicitly provided a superclass; by default our new class will be a subclass of object. By convention, the names of most built-in classes, like object, begin with lowercase letters. All of the other classes that we will define should begin with uppercase letters; hence, our name of Point. We've also provided a minimal...