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

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

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