Attributes

Understanding the data that lives inside your objects.

Introduction

You now know how to create classes and objects. But objects need data -- information that describes them. This data is stored in attributes. Attributes are the characteristics or properties of an object.

What are Attributes?

Attributes are like the characteristics that describe something. Think about a dog: it has a name, a breed, and an age. Think about a car: it has a brand, a color, and a speed. These are all attributes.

Simple way to think about it: Attributes answer the question "What does this object have?" A student has a name, an age, and a grade. Those are its attributes.

Setting Attributes on Objects

You can add attributes to an object after creating it using dot notation:

class Student:
    pass

s1 = Student()
s1.name = "Alice"
s1.age = 20

s2 = Student()
s2.name = "Bob"
s2.age = 22

print(s1.name, s1.age)  # Alice 20
print(s2.name, s2.age)  # Bob 22

Here, each student object gets its own name and age. The attributes belong to the individual object, not to the class.

Why This Approach Has Problems

While setting attributes manually works, it has some issues:

class Student:
    pass

s1 = Student()
s1.name = "Alice"
# Oops! We forgot to set s1.age

s2 = Student()
s2.name = "Bob"
s2.age = 22
s2.email = "bob@email.com"  # s1 doesn't have this!

# This will cause an error:
# print(s1.age)  # AttributeError!

Preview: In Week 3, you will learn about the __init__ constructor, which automatically sets attributes when an object is created. This solves the problem of forgetting to set attributes or having inconsistent objects.

Accessing and Modifying Attributes

You can read and change attributes at any time using dot notation:

class Student:
    pass

s1 = Student()
s1.name = "Alice"
s1.age = 20

# Access an attribute
print(s1.age)    # 20

# Modify an attribute
s1.age = 21
print(s1.age)    # 21

# Add a new attribute anytime
s1.grade = "A"
print(s1.grade)  # A

OOP Terms

  • Attribute: A variable that belongs to an object. It stores data about that object. Also called a property or field.
  • Dot notation: The syntax object.attribute used to access or set an attribute on an object.

Real-World Connection

Think about a bank account. Its attributes might include:

  • owner -- who the account belongs to
  • balance -- how much money is in the account
  • account_number -- the unique identifier
  • account_type -- savings, checking, etc.

Each bank account object would have its own values for these attributes.

Best Practices

  • Use clear, descriptive names for attributes: student.name is better than student.n.
  • Be consistent -- if all students should have a name and age, make sure every student object has both.
  • Use snake_case for attribute names: account_number, not accountNumber.

Common Mistakes

  • Trying to access an attribute that was never set -- this causes an AttributeError.
  • Forgetting to set attributes on some objects but not others, leading to inconsistent data.
  • Confusing class attributes (shared by all) with instance attributes (unique to each object).

Practice Exercises

  1. Create a Book class. Create two book objects and give each a title, author, and pages attribute. Print them all.
  2. Create a Pet class, create an object, set its name and age, then modify the age and print before and after.
  3. Create a Product class with two objects. Give each a name, price, and quantity. Print the total value (price times quantity) for each.

Summary

  • Attributes are data stored inside objects -- they describe what an object has.
  • Use dot notation to set, access, and modify attributes.
  • Each object can have its own unique attribute values.
  • Manually setting attributes works but can lead to inconsistencies -- constructors (Week 3) solve this.