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โ€บDescrittoripredict6 / 161
+100 XP
Compito๐ŸŒ shown in EN
๐Ÿ“ **Task:** Predict the exact output. The \`Tracker\` descriptor prints on bind / get / set. Three things to think about: (1) When does \`__set_name__\` run? (2) Does \`b.x = 10\` go through \`__set__\`? (3) When \`total = b.x + b.y\` runs, which calls fire and in what order? ๐Ÿ“‹ Implement the function above. Tests run automatically. ๐Ÿ’ก **Hint:** Re-read the theory if you get stuck.
Predici output

Leggi il codice attentamente

class Tracker:
    def __set_name__(self, owner, name):
        self.name = name
        print(f"bind:{name}")
    def __get__(self, instance, owner):
        if instance is None:
            return self
        print(f"get:{self.name}")
        return instance.__dict__.get(self.name, 0)
    def __set__(self, instance, value):
        print(f"set:{self.name}={value}")
        instance.__dict__[self.name] = value


class Box:
    x = Tracker()
    y = Tracker()


b = Box()
b.x = 10
b.y = 5
total = b.x + b.y
print(total)

Cosa stamperร  il programma? Scrivi qui: