liveBook · Manning

Attribute

As explained in Practices of the Python Pro

An attribute is data associated with a Python object that represents part of its state.

An attribute is data associated with a Python object. It represents part of the object's state, while methods provide behaviors that can access or change that state. section 2.2.2

In a class:

  • The initializer, __init__, commonly creates the initial attributes. section 2.2.2
  • Related attributes and methods together form the members of a class. section 2.2.2

Rock-paper-scissors state

A simulator initializes self.computer_choice and self.human_choice to None. Its choice methods later assign values to those attributes, and print_choices reads them through self.

How Practices of the Python Pro covers it

How deep it goes
Explains state, initialization, and self-based access with a worked game example, stopping short of deeper attribute mechanics.
Examples
Uses a RockPaperScissorsSimulator with human_choice and computer_choice attributes.
What it assumes
Assumes basic functions and data, then develops the connection between related behaviors, shared inputs, and class state.
What you can do after
Apply it via Python classes by initializing, reading, and updating instance data through self.
How it connects
Connects attributes to classes, objects, methods, instance state, and the __init__ initializer.

Also explained in

Compare how 26 books explain attribute →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practices of the Python Pro. The section links point to the parts of the book it draws on.