December 2, 2026•3 min read

Why Does Your Python Function Change Nothing?

Three lines, three reasons the change disappears: a default list every call shares, a name rebound instead of a dict written, and a counter that is read but never set. All three look correct in review.

Read in Indonesian

In Python, total = total + harga rebinds the local name total. It does not write to the dict you passed in, so the caller still sees 0. The same mistake wears three costumes here: a default list that every call shares, a name rebound inside a function, and a module-level counter that is read but never set.

Each block below is a complete file. Run it, then read the line that surprises you.

What does total = total + harga actually do?

PYTHON
pesanan = {"total": 0}

def tambah_item(pesanan, harga):
    total = pesanan["total"]
    total = total + harga

tambah_item(pesanan, 15000)
print(pesanan["total"])
Output
$ python3 rebind.py
0

Inside the function, total is a new local binding. pesanan["total"] is a lookup, it returns 0, and the sum goes into the local, which disappears when the function returns. Writing back is a different operation: pesanan["total"] = pesanan["total"] + harga mutates the dict. Mutation is the only thing the caller can see.

Why do all the receipts hold the same items?

PYTHON
def pesanan_baru(meja, item=[]):
    return {"meja": meja, "item": item}

meja1 = pesanan_baru(1)
meja3 = pesanan_baru(3)
meja1["item"].append("kopi")
print(meja1["item"], meja3["item"])
print(meja1["item"] is meja3["item"])
Output
$ python3 shared.py
['kopi'] ['kopi']
True

A default argument is evaluated once, when the def runs, not at every call. The list is built once, and every call that omits item receives that same object — so appending at table 1 appends at table 3. A default of None with the list created inside the function is the fix, and it is the only one that survives the next reader.

Why is the daily till still zero?

PYTHON
TOTAL_KAS = 0

def catat_penjualan(jumlah):
    total = TOTAL_KAS + jumlah
    return total

catat_penjualan(15000)
print(TOTAL_KAS)
Output
$ python3 till.py
0

total is local here too, because nothing in the function assigns to the module name. The global is read, the sum is returned, and the caller drops it. Either declare global TOTAL_KAS and assign, or return the new value and let the caller write it — the second is easier to test, because the function stops owning state it does not need.

What do the three fixes look like together?

PYTHON
def pesanan_baru(meja, item=None):
    if item is None:
        item = []
    return {"meja": meja, "item": item, "total": 0}

def tambah_item(pesanan, nama, qty=1):
    harga = MENU[nama]
    pesanan["item"].append({"nama": nama, "qty": qty, "harga": harga})
    pesanan["total"] = pesanan["total"] + harga * qty
    catat_penjualan(harga * qty)
    return pesanan

def catat_penjualan(jumlah):
    global TOTAL_KAS
    TOTAL_KAS = TOTAL_KAS + jumlah
    return TOTAL_KAS
Output
$ make check | grep LULUS
[LULUS] 1. tiap nota cuma punya isi pesanan meja itu
  [LULUS] 2. total nota dihitung dari itemnya
  [LULUS] 3. kas harian bertambah sebesar penjualannya

$ make demo | grep -E 'total nota|Kas harian'
meja 1  total nota: Rp30.000
  meja 3  total nota: Rp8.000
  meja 5  total nota: Rp12.000
Kas harian: Rp50.000

When is rebinding the right thing?

Whenever the value stays inside the function. total = total + harga is correct if the next line returns or prints total — the bug is only that the caller expected the dict to change. The test is who owns the value after the call: if nobody outside needs it, rebinding is cheaper than a parameter object and easier to read.

What bites is the distance between the name you read and the object you actually change. Same syntax, two different results, and nothing on the line tells you which one you got.

Three receipts, three different totals, one till. The full story is Every Receipt Holds the Same Order, the walkthrough answer is The Default Value Is Only Created Once, and the code is inva-lab 0004.

The rule worth keeping: before you write an assignment, ask which object you are changing. If the answer is "the name", the caller will never see it.

Get better at shipping code.

Join the other developers getting tech updates, tips, and relaxed tutorials in their inbox every week.

Stay in the loop

Tech updates, coding tips, and relaxed insights straight to your inbox every week.

No spam. Unsubscribe anytime.

Read next

See all
WebhookEvery Write Comes Back as News

A supplier file says the mug is gone, the app drafts it, the store puts it back, forty times. Seven checks, and the eager webhook handler is only the second problem.

WebhookThe Status That Changed Itself

A product's status blinks between published and draft, nothing errors, and the guard you wrote first works right up until it doesn't. The loop was never an echo.

For developers who learn by struggling

© 2026 Inva.dev.