O'Reilly logo

Python: Essential Reference, Third Edition by David Beazley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required