Class Instances

Instances of a class are created by calling a class object as a function. This first creates a new instance by calling the static method __new__(), which is rarely defined by the user, but implemented as part of object. This, in turn, calls the __init__() method of the class, which is almost always defined by a user to initialize the contents of an instance. For example:

# Create a few accounts
a = Account("Guido", 1000.00)  # Invokes Account.__init__(a,"Guido",1000.00)
b = Account("Bill", 10.00)

The attributes and methods of the newly created instances are accessible using the dot (.) operator as follows:

a.deposit(100.00) # Calls Account.deposit(a,100.00) b.withdraw(50.00) # Calls Account.withdraw(b,50.00) name = a.name # Get ...

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.