DEV Community

Cover image for Quark’s Outlines: Python Object Value
Mike Vincent
Mike Vincent

Posted on

Quark’s Outlines: Python Object Value

Overview, Historical Timeline, Problems & Solutions

An Overview of Python Object Value

What is an object’s value in Python?

In Python, every object has a value. The value is the content the object holds. It is what you get when you use the object in a calculation, comparison, or output.

The value of an object may change or stay fixed, depending on the object’s type. If the object is mutable, its value can change. If the object is immutable, its value stays the same once created.

Python lets you get and compare values of objects.

a = [1, 2]
b = [1, 2]
print(a == b)  # True — same value
Enter fullscreen mode Exit fullscreen mode

These two lists are different objects, but their values match.

What is the difference between value and identity?

Two objects can have the same value but different identities. The value is what the object holds. The identity is where the object lives in memory.

Use the == operator to compare values. Use the is operator to compare identities. Python defines what value means for each type using methods like __eq__.


A Historical Timeline of Python Object Value

Where do Python’s ideas about value come from?

The value of an object is a basic idea from early programming: a container holds something. Python builds on this and expands how values are stored, changed, and compared.

People built ideas of value-based behavior.

1960 — Named values in code, ALGOL used named variables to hold values directly.

1972 — Value copying and comparison, C used equality and pointers to distinguish value from address.

1980 — Immutable types in ABC, gave early examples of strings and numbers as fixed-value objects.

People defined value rules in Python.

1991 — Equality vs identity, Python 0.9.0 separated == from is to compare value vs memory.

2001 — Mutable and immutable types, Python 2.2 explained which objects could change value.

2008 — Unicode strings as value holders, Python 3.0 changed string model to support rich text values.

People gave value more structure.

2016 — Rich comparison operators, Python 3.5 improved how types define ==, <, and !=.

2023 — Type-aware comparisons, Python maintained clear separation between value and identity.


Problems & Solutions with Python Object Value

How does value help you compare, update, and store information?

Each object in Python carries a value. That value may be fixed or changeable. These examples show how Python object value helps solve real tasks in code.


Problem 1: Comparing two things for content

Problem: You get two boxes with the same gift inside. You want to know if the gifts are the same, not if the boxes are the same.

Solution: Use == to compare the value inside each object.

Python lets you compare object values with ==.

gift1 = [1, 2, 3]
gift2 = [1, 2, 3]
print(gift1 == gift2)  # True
Enter fullscreen mode Exit fullscreen mode

These lists are not the same object, but their values match.


Problem 2: Updating a changeable object

Problem: You keep a notebook. You can cross out a word and write a new one. The notebook stays the same, but the words change.

Solution: A mutable object can have its value changed without changing its identity.

Python lets mutable objects change value over time.

notebook = ["first", "second"]
notebook[0] = "new"
print(notebook)  # ['new', 'second']
Enter fullscreen mode Exit fullscreen mode

The list changed its value but kept the same identity.


Problem 3: Using fixed-value types

Problem: You carve your name in stone. You cannot change the stone once it’s written.

Solution: An immutable object like a string or number cannot change its value once created.

Python uses immutable objects for fixed values.

name = "Ada"
new_name = name.upper()
print(name)      # "Ada"
print(new_name)  # "ADA"
Enter fullscreen mode Exit fullscreen mode

The .upper() method returns a new string. The original stays the same.


Problem 4: Grouping other values

Problem: You keep toys in a box. You may take out or add more toys. The box’s value changes depending on its contents.

Solution: A container like a list or dict holds values. These values can change, which changes the container’s value too.

Python lets container objects hold and change other values.

box = []
box.append("toy")
print(box)  # ['toy']
Enter fullscreen mode Exit fullscreen mode

The value of the list changed when an item was added.


Problem 5: Avoiding shared changes

Problem: You send a document to a friend. You both edit your own version. You want your edits to stay separate.

Solution: Use a copy to get a new object with the same value. Then change them separately.

Python lets you make value copies to avoid shared change.

original = [1, 2, 3]
copy = original.copy()
original[0] = 9
print(copy)  # [1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

The copy keeps the original value, even after the first list changes.


Like, Comment, Share, and Subscribe

Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments, too! If you want to see more content like this, don't forget to subscribe to my channel. Thanks for reading!


Mike Vincent is an American software engineer and writer based in Los Angeles. More about Mike Vincent

Top comments (0)