Concepts / Accessing Attributes and Methods

Accessing Attributes and Methods

An object bundles data (attributes) and behavior (methods) into a single, organized unit.

  • Programming

From Blueprint to Object

An object is a single organized unit that combines data and behavior. The data is held in attributes, while the behavior is provided by methods. To understand where an object comes from, begin with the distinction between a class and an object: a class is a blueprint, and an object is an instance created from that blueprint.

createscreatesBankAccount classblueprintAccount onebalance: 500 dollarsAccount twobalance: 2000 dollars
How does one class definition relate to the individual objects created from it?

The class describes what its objects will contain and what they will be able to do. Once the class exists, many objects can be created from it. These objects follow the same blueprint, but each object is independent and can hold different values. For example, two bank account objects can both support depositing, withdrawing, and checking a balance while having different balances.

The Object as a Unified Unit

A useful way to understand an object is to compare it with a collection of loose program parts. A car system might otherwise keep color, speed, and mileage in separate variables and keep accelerating, braking, and status-display operations in separate functions. An object brings these related pieces together. Its attributes describe the car, and its methods describe what the car can do.

containscontainscontainssupportssupportssupportsCar objectone organized unitcolorattributeacceleratemethodspeedattributebrakemethodmileageattributedisplay statusmethod
What data and behavior are contained within a single object?

Bundling related data and behavior gives you one self-contained thing to work with instead of a set of scattered variables and functions. This organization makes code easier to understand, maintain, find bugs in, and reuse.

Attributes as Object Data

An attribute is a piece of data that belongs to an object. It stores information about the object's state or characteristics.

A class specifies which attributes objects created from it will have. Each individual object then holds its own values for those attributes. A Student class, for example, might define name, student_id, and gpa. Every student object can have those kinds of information, but one student's values are independent of another student's values.

storesstoresstoresStudent objectone instancenamethis student's namestudent_idthis student's identifiergpathis student's grade value
Where are an object's attributes represented, and how does working with an attribute retrieve information about that object?

Separating Two Instances

Imagine creating two bank account objects from the same BankAccount class.

Use one blueprint: Both objects are created from the BankAccount class, so both are organized around an account number, an owner's name, a balance, and account actions.

Give each object its own data: The first account can have a balance of 500 dollars, while the second can have a balance of 2000 dollars.

Compare the instances: The objects support the same kinds of actions, but their balances are independent because they are separate instances.

One class can produce multiple independent objects with the same structure and different attribute values.

Methods as Object Behavior

A method is a function that lives inside an object. While an attribute stores data, a method performs an action. Methods typically work with the object's attributes to do something useful.

A method can use an attribute to produce a result, or it can change an attribute when the object's state needs to be updated. For a Student object, calculate_letter_grade can use the gpa attribute to return a letter grade. An update_gpa method can change the gpa attribute when a new grade is recorded. The attribute is the stored information; the method is the object's way of doing something with that information.

hascan callupdateshasStudent objectbefore update_gpagpaprevious valueupdate_gpamethodgpaupdated valueStudent objectafter update_gpa
What happens to an object's attributes when one of its methods is called?

Organizing with class

The class keyword marks the beginning of a blueprint in Python. It is followed by a class name, a colon, and an indented block. Inside that block, the related attributes and methods are grouped together as the contents of the class.

The class is more than a label for a collection of code. It is the organizational container that gives related data and behavior a coherent structure. For the bank account model, the account number, owner's name, balance, deposit action, withdrawal action, and balance-checking action belong together in the BankAccount class. When the account model needs a new feature, the class gives you a clear place to look.

namesfollowed bycontainscontainsclassstarts a blueprintBankAccountblueprint name:begins the blockaccount dataattributesaccount actionsmethods
How does a class definition group an object's data and behavior into one reusable structure?

Common Misunderstandings

  • Treating the class and the object as identical

    The class is the blueprint, while each object is an independent instance created from that blueprint.

    Fix: Describe the class as the plan and the object as one created instance that holds its own values.

  • Calling an attribute an action

    Balance is information stored by the account, so it is an attribute. Depositing, withdrawing, and checking the balance are actions, so they are methods.

    Fix: Ask whether the item stores information or performs an action. Use attribute for stored information and method for behavior.

  • Assuming all objects from one class have identical data values

    Instances follow the same blueprint but are independent and can have different attribute values.

    Fix: Separate shared structure from individual data. Objects can support the same actions while holding different values.

  • Keeping related variables and functions scattered

    The related data and behavior become harder to understand and maintain when they are not organized as one coherent unit.

    Fix: Use a class to group the related attributes and methods into a reusable structure.

Practice the Distinction

EASY

Consider a BankAccount model with an account number, an owner's name, a balance, and actions for depositing, withdrawing, and checking the balance. Classify each item as either an attribute or a method. Then explain why two account objects created from the same class can have different balances while still supporting the same actions.

Hints
  • Attributes store information about an object.
  • Methods perform actions.
  • The class is the blueprint; each account object is an independent instance.
  1. Identify the blueprint: name the class that organizes the model.
  2. List the stored information: these are the object's attributes.
  3. List the actions: these are the object's methods.
  4. Compare instances: check which values can differ from one object to another.
  5. Explain the benefit: describe how grouping the pieces improves organization and reuse.

Key Takeaways

  1. A class is a blueprint, and an object is an independent instance created from that blueprint.
  2. Attributes store an object's information, such as a student's name or a bank account's balance.
  3. Methods are functions inside an object that perform actions, often using or changing the object's attributes.
  4. The class keyword groups related data and behavior into a coherent structure.
  5. Bundling attributes and methods makes code easier to understand, maintain, debug, and reuse.

Key Takeaways

  • A class defines the structure and behavior that its objects will have.
  • Each object is an independent instance with its own attribute values.
  • Attributes represent stored data, while methods represent actions.
  • The class keyword organizes related attributes and methods into one reusable unit.