liveBook · Manning

Attribute

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

An attribute is a named value associated with a Python object or class and accessible through attribute lookup.

An attribute is a named value associated with an object or class. In a Person instance, attributes such as fname, mname, and lname hold that person’s state; full_name() reads them to produce a name. section 4.1.1

class Person:
    def __init__(self, fname: str, mname: str = None, lname: str = None):
        self.fname = fname
        self.mname = mname
        self.lname = lname
 
    def full_name(self) -> str:
        full_name = self.fname
        if self.mname is not None:
            full_name = f"{full_name} {self.mname}"
        if self.lname is not None:
            full_name = f"{full_name} {self.lname}"
        return full_name

See how the constructor assigns name attributes to self and how full_name() uses them. section 4.1.1

Plain attributes are the default for class design here. Python attributes and methods are public; properties can provide control over how attributes are accessed and used. section 4.1.1

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Explains instance attributes through self and a method that reads them, while deferring property-based access control.
Examples
Demonstrates fname, mname, and lname attributes in a Person class and uses them to form full names.
What you can do after
Apply plain attributes to store per-instance data and read it from a method.
How it connects
Connects attributes with classes, instances, 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 Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.