liveBook · Manning

Object

As explained in Get Programming: Learn to code with Python

A Python runtime entity that combines associated data with operations for interacting with that data.

In Python, everything you create is an object. An object has a type, associated data, and operations you can use to interact with it. Think of a word: its letters are data, and turning them uppercase is an operation. Different types offer different data and operations. section 3.1 section 4.2

A variable gives an object a name so you can refer to it later. In `greeting = "hello"`, `greeting` is the name; the string is the object. An expression can produce an object too: `a = 1 + 2` binds `a` to the resulting value, 3. section 4.2

Objects differ in whether their values can change. Strings and integers are immutable; lists and dictionaries are mutable. Two variables can refer to the same mutable object, so a change made through either name is visible through both. Copying the object is different from giving it another name. section 24.3 section 28.0

A class defines an object type by specifying its data attributes and methods—operations you can call on it. A particular object created from a class is an instance. Dot notation selects an attribute or calls a method on a particular instance. section 31.4 section 31.7

Two objects of one type

`one_circle = Circle()` `another_circle = Circle()` `one_circle.change_radius(4)` These are two instances of the same class. The method call changes only `one_circle`: its radius becomes 4, while `another_circle` keeps its radius of 0. The object before the dot tells Python which instance the method acts on. Quick check: which circle would `another_circle.get_radius()` ask for a radius? section 31.4

Figure 31.1. On the left are the data attributes of two circle objects. On the right, you can see that one data attribute changed after using dot notation on it to change the value.

Compare the two circles’ data attributes before and after changing one radius to see why the other instance stays unchanged. section 31.4

You can define your own types to keep related properties and behaviors together, then create objects and call their methods in a larger program, such as a card game. The class definition describes what those objects can do; the gameplay uses particular objects to do it. section 32.3 section 34.7

How Get Programming: Learn to code with Python covers it

How deep it goes
Works through instance creation, independent attributes, dot notation, and how method calls supply the instance as self.
Examples
Uses two Circle instances with different radii, a Door class exercise, and objects in a card game.
What it assumes
Introduces objects through familiar things before teaching variable names, types, instances, and classes.
What you can do after
Build it from scratch by defining a class, creating instances, and calling their methods in a program.
How it connects
Connects variables and types to mutability, aliasing, classes, methods, and dot notation.

Also explained in

Compare how 4 books explain object →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.