Vai al contenuto principale
๐Ÿ”’ Modalitร  anteprima. Le prime quindici lezioni di Foundations sono gratuite; questa รจ Pro. Avvia un trial di 7 giorni per sbloccare l'editor, i suggerimenti AI e il resto del programma. Carta richiesta, disdici in qualsiasi momento dalla Dashboard.Avvia trial di 7 giorni โ†’
โšก
โ† Corsiโ€บSenior Deep-DivesModulo 1 ยท Concorrenza e aspetti interni asincroniโ€บConteggio della memoria e dei riferimentipredict7 / 161
+100 XP
Compito๐ŸŒ shown in EN
๐Ÿ“ **Task:** Predict the 4-line refcount trace. Each print fires after a binding event โ€” bind, del, append-to-list, then drop the whole list. ๐Ÿ“‹ Implement the function above. Tests run automatically. ๐Ÿ’ก **Hint:** Re-read the theory if you get stuck.
Predici output

Leggi il codice attentamente

# Deterministic refcount tracker โ€” mirrors CPython behaviour but
# without sys.getrefcount's baseline-by-1 quirk. Every Python
# binding increments; every del / list-removal decrements.

class Refcount:
    def __init__(self, name):
        self.name = name
        self.count = 1   # the original binding

    def bind(self):          # `y = x`
        self.count += 1
    def unbind(self):        # `del y`
        self.count -= 1
    def append_to(self, container_name):
        self.count += 1
    def container_dropped(self, n):
        self.count -= n

obj = Refcount("payload")    # 1 โ€” the original assignment
y = obj; obj.bind()          # rebind โ†’ 2
print(obj.count)

obj.unbind()                 # del y โ†’ 1
print(obj.count)

big_list = ["item"]
big_list.append(obj); obj.append_to("big_list")   # +1 โ†’ 2
big_list.append(obj); obj.append_to("big_list")   # +1 โ†’ 3
print(obj.count)

obj.container_dropped(2)     # del big_list โ€” list had 2 refs to obj
print(obj.count)

# What does this print? Type your prediction.

Cosa stamperร  il programma? Scrivi qui: