liveBook · Manning

Attribute

As explained in The Quick Python Book, Fourth Edition

An attribute is a named value or behavior associated with an object and accessed through that object.

An attribute is a named value or behavior accessed through an object. In Python, dot notation reads an attribute, assigns a value, or calls a method: `circle.radius`, `circle.radius = 5`, or `circle.area()`. section 15.1.1 section 3.6

class Circle:
    pass
 
my_circle = Circle()
my_circle.radius = 5
print(2 * 3.14 * my_circle.radius)
 
31.4

The `radius` attribute is created by assigning to the `Circle` instance. Reading it through the same instance supplies the value used in the calculation. section 15.1.1

An instance variable belongs to a particular object. Assigning `self.radius = 1` in `__init__` gives each new instance its own `radius`; assigning `circle.radius = 5` later changes that instance. Inside a method, write `self.radius` to reach the attribute. Bare `radius` refers to a local variable instead. section 15.2 section 15.1.1

Two attribute lookups deserve care:

  • If an instance has no attribute of a given name, lookup can return a class variable with that name. Assigning through the instance creates an instance variable instead of changing the class variable; assign through the class to change the shared value. section 15.4.1
  • A subclass instance can access inherited methods and class variables. Methods defined in either the parent or child that use `self.x` on that instance refer to the same instance variable. section 15.7

Use a property when ordinary dot access should run code. A method decorated with `@property` supplies the value when the attribute is read. Without a setter, the property is read-only; a method decorated with `@name.setter` handles assignment. This lets a plain instance variable become a computed or checked attribute without changing how callers access it. section 15.10

A computed temperature attribute

A `Temperature` object can store Fahrenheit in `self._temp_fahr` while exposing Celsius as `temp`. Its `@property` getter converts the stored value on a read; its `@temp.setter` converts a Celsius assignment back to Fahrenheit. With `_temp_fahr` initially `0`, reading `t.temp` gives about `-17.78`; after `t.temp = 34`, reading `t.temp` gives `34.0`. Both operations retain ordinary dot syntax. section 15.10

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains creation and lookup of instance and class attributes, inherited access, and property getters and setters without descending into lookup internals.
Examples
Demonstrates a circle’s radius, class-variable shadowing, inherited instance variables, and a temperature property that converts units.
What it assumes
Assumes general programming familiarity but does not require prior knowledge of a particular object-oriented language or paradigm.
What you can do after
Build it from scratch with instance attributes and properties, while avoiding accidental class-variable shadowing.
How it connects
Connects attributes to instance variables, class variables, inheritance, methods, and properties.

Also explained in

Compare how 26 books explain attribute →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.