liveBook · Manning

Attribute

As explained in Get Programming: Learn to code with Python

An attribute is a named property, value, or piece of information associated with an entity, object, or program element.

An attribute is a piece of data that belongs to an object. It describes a characteristic of that object: a circle might have a radius, while a rectangle might have a length and a width. Attributes are also called object properties or data attributes. They differ from behaviors, which are actions an object can perform. section 30.2 section 31.2

When you create a class, decide which attributes each object needs. Its `__init__` method usually gives them their starting values. Write `self.` before an attribute name to show that the value belongs to the object. Each object created from the class has its own values. section 31.2

A rectangle’s attributes

A `Rectangle` can start with `length` and `width`. In `__init__(self, length, width)`, the assignments `self.length = length` and `self.width = width` store the supplied values as attributes. For `Rectangle(2, 4)`, those values are 2 and 4. The parameters are temporary; the attributes remain with the rectangle after `__init__` finishes. Passing only one value produces an error because both are required. section 31.5

Listing 31.1. A Rectangle class
class Rectangle(object):
    """ a rectangle object with a length and a width """
    def __init__(self, length, width):
        self.length = length
        self.width = width
    def set_length(self, length):
        self.length = length
    def set_width(self, width):
        self.width = width

See the complete `Rectangle` class: `__init__` stores two attributes, and methods can change them. section 31.5

Dot notation lets you refer to an object’s property or call one of its methods. For example, `a_rectangle.length` refers to a rectangle’s length; `a_rectangle.set_length(3)` calls a method that changes it. The dot connects the name to that particular object. section 31.5 section 30.3

Choosing an attribute

A stack can keep its items in one list attribute, initialized with `self.stack = []`. Methods then add or remove items using that list. Quick check: if you made a circle class, which attribute would you choose to describe its size? A radius is one option. section 32.1 section 30.2

How Get Programming: Learn to code with Python covers it

How deep it goes
Explains attribute selection, initialization with `self`, per-object values, and the difference between persistent attributes and temporary parameters.
Examples
Uses a circle’s radius, a rectangle’s length and width, and a stack’s list of items.
What it assumes
Builds on familiar object types such as lists while introducing class-defined data attributes and their initialization.
What you can do after
Build it from scratch by choosing attributes for a Python class and initializing them with `self` in `__init__`.
How it connects
Connects attributes to classes, object properties, methods, `__init__`, and dot notation.

Also explained in

Compare how 26 books explain attribute →

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.