Concepts / Classes and Objects in Python

Classes and Objects in Python

In Python, everything used in a program is generically called an object—numbers, strings, functions, and more.

  • 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.

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.

refers toscorevariable name10object in memory
What is the difference between a variable name and the object it refers to in memory?

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

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?

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

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

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

Keep the roles distinct: Do not describe the variable as containing the number. Describe the variable as referring to the number object.

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.

refers torefers tofirst_namevariable namenumber objecta value in memorysecond_namevariable name
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

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.

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

  • Treating a variable as if it contains the object itself.

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

    Fix: Say that the variable refers to the object.

  • Assuming that only complex values are objects.

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

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

  • Confusing two variable names with two objects.

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

    Fix: 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. 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.

Key Takeaways

  • 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.