liveBook · Manning

Behavior

As explained in Get Programming: Learn to code with Python

An object behavior is an operation that defines how objects of a type can act or interact.

An object behavior is an operation that defines how objects of a type can act or interact. A calculator performs mathematical operations, while a cat might scratch, hide, or sleep. Properties describe what an object has; behaviors describe what it can do. section 30.0 section 30.2

When designing an object type, choose behaviors that represent useful interactions:

  • A car can change color, make a noise, or turn its wheels. section 30.2
  • A circle can report its area or circumference. section 30.2
  • A map point can identify its country or calculate distance from another point. section 30.2
  • A room can add an item, making interaction with its stored objects explicit. section 30.2

Behavior through a class

A Python class packages properties and behaviors into one reusable object type. For a queue, properties can hold its items, while behaviors can get its size, add one or many items, remove one or many items, and display the queue. A program can then create queue objects and perform a sequence of operations on them. section 31.0 section 32.3

Objects of the same type share the behaviors defined for that type. In Python, dot notation commonly accesses an object's behaviors, such as list operations including append, extend, pop, index, and remove; number operations commonly use mathematical symbols instead. section 30.4 section 31.0

How Get Programming: Learn to code with Python covers it

How deep it goes
Defines behaviors as operations and illustrates them with cars, circles, map points, rooms, classes, and methods.
Examples
Uses calculators, cats, cars, circles, map points, rooms, lists, numbers, and a queue to demonstrate operations.
What it assumes
Builds from familiar Python objects and introduces object properties, behaviors, classes, and dot notation as connected ideas.
What you can do after
Apply it via Python classes by choosing behaviors, implementing methods, creating objects, and performing operation sequences.
How it connects
Connects behaviors with object types, properties, classes, methods, dot notation, and data structures.

Also explained in

Compare how 4 books explain behavior →

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.