You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

11.2 Classes and instances

3 min readjune 24, 2024

Classes in Python are blueprints for creating objects, encapsulating data and behavior. They define attributes and methods that of the will have, allowing for organized and reusable code in .

Instances are individual objects created from a , each with its own set of attributes. By using classes and instances, programmers can model real-world entities and their interactions, making code more intuitive and easier to maintain.

Classes and Instances

Class definition with constructor

Top images from around the web for Class definition with constructor
Top images from around the web for Class definition with constructor
  • Class serves as a blueprint or template for creating objects (an example of object-oriented programming)
    • Defined using
      class
      keyword followed by class name ( convention)
    • Example class names:
      Person
      ,
      Car
      ,
      BankAccount
  • [__init__()](https://www.fiveableKeyTerm:__init__())
    is a special constructor that initializes attributes
    • Called automatically when a new class instance is created
    • Takes
      [self](https://www.fiveableKeyTerm:self)
      as the first parameter, referring to the instance being created
    • Additional parameters can be defined to accept values for instance attributes
      • Example:
        def __init__(self, name, age)
        to initialize
        name
        and
        age
        attributes
  • Instance attributes are variables specific to each instance of a class
    • Defined within
      __init__()
      using
      self.attribute_name
      syntax
    • Each instance has its own copy of instance attributes
      • Example:
        self.name = name
        and
        self.age = age
        inside
        __init__()
  • Instance methods are functions defined within a class that operate on instances
    • Defined like regular functions, but with
      self
      as the first parameter
    • Can access and modify instance attributes using
      self
      keyword
      • Example:
        def introduce(self): print(f"My name is {self.name} and I am {self.age} years old.")

Creation of class instances

  • Instances are created by calling the class name followed by parentheses
    • Arguments can be passed to
      __init__()
      to initialize instance attributes
      • Example:
        person1 = Person("Alice", 25)
        creates a
        Person
        instance with
        name
        "Alice" and
        age
        25
  • Instance attributes can be accessed and modified using
    • Syntax:
      instance_name.attribute_name
      • Example:
        person1.name
        accesses the
        name
        of
        person1
  • Instance methods can be called on instances using dot notation
    • Syntax:
      instance_name.method_name(arguments)
      • Example:
        person1.introduce()
        calls the
        introduce
        method on
        person1
  • Multiple instances of a class can be created, each with its own set of instance attributes
    • Example:
      person2 = Person("Bob", 30)
      creates another
      Person
      instance with different attribute values

Instance vs class attributes

  • Instance attributes are specific to each instance of a class
    • Defined within
      __init__()
      using
      self.attribute_name
    • Each instance has its own copy of instance attributes
      • Example:
        self.name
        and
        self.age
        are instance attributes
    • Accessed and modified using dot notation on the instance:
      instance_name.attribute_name
      • Example:
        person1.name = "Alice"
        modifies the
        name
        attribute of
        person1
  • Class attributes are shared by all instances of a class
    • Defined directly within the class, outside of any methods
    • All instances of the class have access to the same class attributes
      • Example:
        species = "Human"
        defined directly in the
        Person
        class
    • Accessed using dot notation on the class itself:
      ClassName.attribute_name
      • Example:
        Person.species
        accesses the
        species
    • Can also be accessed through instances:
      instance_name.attribute_name
      • Example:
        person1.species
        also accesses the
        species
  • Uses of instance attributes:
    • Storing data specific to each instance (
      name
      ,
      age
      , etc.)
    • Representing the state or properties of an object
  • Uses of class attributes:
    • Storing data shared among all instances of a class (
      species
      , default values, etc.)
    • Defining constants or default values for the class
  • Modifying class attributes affects all instances, while modifying instance attributes only affects the specific instance
    • Example: Changing
      Person.species
      affects all
      Person
      instances, but changing
      person1.name
      only affects
      person1

Object-Oriented Programming Concepts

  • : Bundling data and methods that operate on that data within a single unit (class)
  • : Allows a class to inherit attributes and methods from another class
  • : Ability of different classes to be treated as instances of the same class through a common interface
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary