liveBook · Manning

Attribute

As explained in Python Workout: 50 ten-minute exercises

An attribute is a named value or behavior associated with a Python object or class and accessed through attribute syntax.

An attribute is a named value or behavior attached to an object, class, or module and accessed with dot syntax. For example, a Scoop instance can expose flavor, a class can expose max_scoops, and the os module exposes sep. Attributes let code keep data and operations close to the objects they describe. section 8.0 section 9.2

Use attributes in several related ways:

  • Create an instance attribute in __init__, commonly by assigning self.flavor = flavor. Each instance then carries its own value. section 9.1 section 9.2
  • Define a class attribute when one value belongs to the class as a whole. Bowl.max_scoops communicates a shared limit and avoids repeating a hardcoded value. section 9.11 section 9.12
  • Access module attributes through a module name, such as os.sep, or import selected attributes into the current namespace. section 8.0
  • Store other objects as attributes to express composition: a Bowl keeps Scoop instances in self.scoops rather than storing only their flavor strings. section 9.7 section 9.10

Attribute lookup follows the ICPO rule: Python checks the instance, then its class, then parent classes. Thus self.max_scoops lets one method work for Bowl and BigBowl while resolving 3 or 5 from the appropriate class. An instance may also shadow a class attribute by defining an attribute with the same name. section 9.12 section 9.17

Instance and class attributes

class Bowl: max_scoops = 3 def __init__(self): self.scoops = [] bowl = Bowl() print(bowl.max_scoops) # found on Bowl print(bowl.scoops) # found on bowl class BigBowl(Bowl): max_scoops = 5 big = BigBowl() print(big.max_scoops) # 5 A method that tests len(self.scoops) against self.max_scoops automatically uses the subclass limit without duplicating the method. section 9.12 section 9.17

Attributes can also represent behavior. Methods are attributes defined on classes, while special attributes such as __init__ and __repr__ use double underscores. Calling an instance method supplies the instance through the conventional first parameter, self. section 9.2 section 9.7

For data-focused classes, @dataclass reduces attribute-assignment boilerplate. Declare flavor: str at class level and the decorator supplies an initializer; annotations remain optional generally but are required for dataclass fields. External tools such as Mypy can check the declared types. section 9.10

@dataclass
class Scoop():
   flavor : str

Inspect the compact dataclass definition to see how a declared field becomes an instance attribute without writing __init__. section 9.10

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Explains instance, class, module, special, and method attributes, then works through ICPO lookup, inheritance, composition, and dataclasses.
Examples
Uses Scoop.flavor, Bowl.scoops, Bowl.max_scoops, os.sep, animal properties, and dataclass fields as concrete examples.
What it assumes
Assumes Python functions and basic classes while teaching object instances, self, __init__, and attribute lookup through worked exercises.
What you can do after
Build it from scratch by defining, reading, and reusing instance and class attributes across composed and inherited Python classes.
How it connects
Connects attributes with objects, classes, instances, methods, modules, composition, inheritance, __init__, and dataclasses.

Also explained in

Compare how 26 books explain attribute →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout: 50 ten-minute exercises. The section links point to the parts of the book it draws on.