Glossary

Instance

An instance is a concrete object created from a class. When you write dog = Dog("Rex"), you create an instance of the Dog class. Each instance has its own set of instance attributes — data specific to that object — while sharing the methods and class attributes defined in the class. The relationship between a class and its instances is "one to many": a single class can have thousands of instances.

Instance attributes are typically set in the __init__ method using self.attribute = value. The self parameter (the first parameter of every regular method) refers to the instance on which the method was called. Python does not enforce access modifiers (public, private, protected) — all attributes are accessible — but the convention is that names starting with an underscore are "internal" and names with double underscores trigger name mangling.

You can check whether an object is an instance of a particular class with isinstance(obj, ClassName), which also returns True for subclasses. The type(obj) function returns the exact class. Instance creation actually involves two steps: __new__ allocates the object and __init__ initialises it, though overriding __new__ is rare outside of immutable types and metaclass programming.

Related terms: Class, Object, Self

Discussed in:

This site is currently in Beta. Please email Chris Paton (cpaton@gmail.com) with any suggestions, questions or comments.