Concepts / Classes and Objects in Python

Classes and Objects in Python

The most important starting point is this: in Python, everything used in a program is generically called an object.

  • Programming

One Idea Behind Many Values

When you work with a number, a string, or a function in Python, you are working with something Python generically calls an object. This gives Python one broad way to describe the values used in a program. Instead of treating simple values as completely separate from more complex values, Python treats them all as objects.

Key point

The most important starting point is this: in Python, everything used in a program is generically called an object.

Names and Objects

A variable is a name or reference that points to an object in memory. The variable does not contain the object itself. This distinction matters because the name and the object play different roles: the name gives your program a way to refer to something, while the object is the value being referred to.

scorevariable name10object in memoryrefers to
What is the difference between a variable name and the object it refers to in memory?
In the real world

Generated example: imagine a label attached to a storage container. The label is not the contents of the container, but it gives you a way to identify those contents. In the same way, a variable name is not the object itself; it refers to the object.

Kinds of Python Objects

Python's object model includes values that beginners often think of as unrelated categories. Numbers are objects. Strings are objects. Functions are objects. Other values used in a program are also objects. The point is not that these values all behave identically; the point is that Python uses the general concept of an object for all of them.

Value categoryHow Python describes it
NumberAn object
StringAn object
FunctionAn object
Other value used in a programAn object

Python applies the general term object to numbers, strings, functions, and more.

Following a Reference

Worked example

Two Names, One Object

Generated example: suppose one variable name refers to a number object, and a second variable name is made to refer to that same object. What should you keep separate in your mental model?

  1. 1
    Identify the names

    There are two variable names. Each name is a reference used by the program.

  2. 2
    Identify the object

    The number is the object. It is not the same thing as either variable name.

  3. 3
    Follow both references

    Both names can refer to the same object. The names remain separate references, while the object they refer to is shared.

  4. 4
    Keep the roles distinct

    Do not describe the variable as containing the number. Describe the variable as referring to the number object.

Result

A variable is a name or reference, and an object is the value to which that reference points. Multiple variable names can refer to the same object.

first_namevariable namenumber objecta value in memorysecond_namevariable namerefers torefers to
How does a variable connect to an object, and what happens when two variables refer to the same object?

What do you think happens?

If two variable names refer to the same object, are the variable names and the object identical?

  • Yes. The names and the object are all the same thing.

  • No. The names are references, while the object is the value being referred to.

Reveal answer
Answer:

No. The names are references, while the object is the value being referred to.

Python distinguishes a variable name from the object in memory that the name refers to. Two names may refer to one object without becoming that object.

Objects Have Three Aspects

Definition

Python is strongly object-oriented. This means that all values, including simple values such as integers, are objects with type, identity, and value.

  • Type: an aspect of every Python value because every value is an object.
  • Identity: an aspect of every Python value because every value is an object.
  • Value: the information represented by the object.

For this lesson, the practical consequence is more important than memorizing terminology: even a simple integer belongs to Python's object model. A variable gives your program a reference to that object, rather than serving as a container that literally contains the object.

What This Lesson Establishes About Classes

The supplied material establishes the object side of the topic: Python treats everything used in a program as an object, and variables refer to objects. It does not provide a definition of a class, instructions for creating a class, or a detailed account of how classes produce objects. Therefore, use this lesson as the foundation for understanding objects and references before studying the additional mechanics of classes.

Key point

Do not let the word class distract from the central relationship established here: a variable name refers to an object, and Python uses the object concept for numbers, strings, functions, and more.

Mistakes with Names and Values

  • Common mistake

    Treating a variable as if it contains the object itself.

    What it looks like

    Describing a variable name as the number rather than as a reference to a number object.

    Why it fails

    The source distinction is that a variable is a name or reference, while the object is the value in memory.

    Do this instead

    Say that the variable refers to the object.

  • Common mistake

    Assuming that only complex values are objects.

    What it looks like

    Thinking that a function can be an object but an integer cannot.

    Why it fails

    Python's object-oriented model includes all values, including simple values such as integers.

    Do this instead

    Remember that numbers, strings, functions, and other values are all objects.

  • Common mistake

    Confusing two variable names with two objects.

    What it looks like

    Assuming that two names must identify two separate objects.

    Why it fails

    More than one variable name can refer to the same object.

    Do this instead

    Trace each name to the object it refers to instead of counting names as objects.

Check Your Mental Model

EASY

For each statement, decide whether it correctly describes Python's object model. Then rewrite any incorrect statement.

  1. A variable is the object stored in memory.
  2. A string is an object in Python.
  3. A function is an object in Python.
  4. Two variable names can refer to the same object.
  5. An integer is excluded from Python's object model because it is a simple value.
Hints
  • Separate the name or reference from the object it refers to.
  • The object concept includes numbers, strings, functions, and more.
  • Simple values such as integers are included.
  1. 1
    A strong answer identifies statements 1 and 5 as incorrect. A variable is a name or reference, not the object itself. An integer is an object, even though it is a simple value. Statements 2, 3, and 4 match the object-and-reference model.

Key Takeaways

  • Python generically calls everything used in a program an object.
  • Numbers, strings, functions, and other values are all objects.
  • A variable is a name or reference that points to an object in memory.
  • The variable does not contain the object itself.
  • Python's strongly object-oriented model gives values a type, identity, and value, supporting flexible and consistent code.

At a glance

  • Everything used in a Python program is generically called an object.
  • Numbers, strings, functions, and simple values such as integers all belong to the object model.
  • A variable is a name or reference, not the object itself.
  • A variable refers to an object in memory, and multiple names can refer to the same object.
  • Understanding objects and references is a foundation for writing flexible, consistent Python code.

Related concepts

More concepts starting with C