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 ...
Get Python Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.