The class Statement

A class defines a set of attributes associated with a collection of objects known as instances. These attributes typically include functions, which are known as methods,variables, which are known as class variables, and computed attributes, which are known as properties.

Classes are defined using the class statement. The body of a class contains a series of statements that is executed when the class is first defined. Here’s an example:

class Account(object): "A simple class" num_accounts = 0 def __init__(self,name,balance): "Initialize a new Account instance" self.name = name self.balance = balance Account.num_accounts += 1 def __del__(self): Account.num_accounts -= 1 def deposit(self,amt): "Add to the balance" self.balance ...

Get Python: Essential Reference, Third Edition 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.