Small Group Tutorials

Here to help students catch up, keep up, and move ahead. Book a consultation here.

How to Master Python functools.cached_property in Punggol Tuition

HDB flats at sunset beside Parc Centros and a side road in Punggol

When a learner can reproduce a familiar example but a small variation causes confusion, the problem is usually an incomplete model rather than a lack of effort. The fastest useful response is to expose the hidden state and test one boundary at a time.

Python functools.cached_property turns a method into an attribute whose value is computed on the first lookup that finds no same-named instance attribute, then written into that instance for ordinary later access. Mastery means predicting exactly when the getter runs, when a write or deletion changes the cache, which objects provide the mutable instance dictionary the descriptor needs, why an exception leaves no cached result, how concurrent first reads may run the getter more than once, and when an explicit cache or ordinary property states the lifecycle more honestly. This guide begins with that mechanism, then develops it through worked traces, deliberate mistakes, explained practice and transfer decisions.

The aim is independent reasoning. A learner should be able to predict behaviour, locate the earliest wrong assumption, use a safe diagnostic procedure and defend a design choice in a new project.

Punggol families can use the guide in short sessions around homework, CCAs and rest. The activities are proposed learning exercises, not claims about a physical branch, timetable, class size, fee, school relationship or guaranteed result.

Use disposable data and repositories, preserve backups, and check version-sensitive details against the official source. Current documentation settles a technical contract; observation and explanation turn that contract into usable knowledge.

Find your next learning step

Choose the route that matches the present difficulty. Use the complete index for a systematic course.

Build the model

Chapters 1-4 . Begin here, then continue after the learner can predict, verify and explain.

Use the core tools

Chapters 5-8 . Begin here, then continue after the learner can predict, verify and explain.

Handle boundaries

Chapters 9-12 . Begin here, then continue after the learner can predict, verify and explain.

Debug and verify

Chapters 13-16 . Begin here, then continue after the learner can predict, verify and explain.

Transfer with judgment

Chapters 17-20 . Begin here, then continue after the learner can predict, verify and explain.

Open the full chapter index . Jump to capstone practice . Use the How Studying Works hub . Read the official documentation

CHAPTER 1 OF 20 . Build the model

1. The descriptor computes on a missing attribute

Back to contents

A cached_property descriptor runs its getter only when normal lookup finds no same-named value in the instance dictionary. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is imagining a background cache that computes during object construction. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for The descriptor computes on a missing attribute, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the The descriptor computes on a missing attribute chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on imagining a background cache that computes during object construction. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

from functools import cached_property
class Report:
    @cached_property
    def total(self):
        print('compute'); return 42
r=Report(); print('before'); print(r.total)

Explained result. Construction prints only before; the first r.total lookup prints compute and returns 42. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: revision summary. Predict the rule using a large set of quiz attempts produces one derived mastery score. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A cached_property descriptor runs its getter only when normal lookup finds no same-named value in the instance dictionary.” Apply this procedure: State the contract for The descriptor computes on a missing attribute, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Construction prints only before; the first r.total lookup prints compute and returns 42. For the revision summary, add one near-miss that exposes imagining a background cache that computes during object construction. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: reading log. Contrast the rule using many entries produce a normalized total only when the report asks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A cached_property descriptor runs its getter only when normal lookup finds no same-named value in the instance dictionary.” Apply this procedure: State the contract for The descriptor computes on a missing attribute, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Construction prints only before; the first r.total lookup prints compute and returns 42. For the reading log, add one near-miss that exposes imagining a background cache that computes during object construction. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: science dataset. Stress-test the rule using measurements produce one expensive calibration summary. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A cached_property descriptor runs its getter only when normal lookup finds no same-named value in the instance dictionary.” Apply this procedure: State the contract for The descriptor computes on a missing attribute, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Construction prints only before; the first r.total lookup prints compute and returns 42. For the science dataset, add one near-miss that exposes imagining a background cache that computes during object construction. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: CCA roster. Explain the rule using participants produce an index used repeatedly after construction. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A cached_property descriptor runs its getter only when normal lookup finds no same-named value in the instance dictionary.” Apply this procedure: State the contract for The descriptor computes on a missing attribute, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Construction prints only before; the first r.total lookup prints compute and returns 42. For the CCA roster, add one near-miss that exposes imagining a background cache that computes during object construction. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers imagining a background cache that computes during object construction.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for The descriptor computes on a missing attribute, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from The descriptor computes on a missing attribute?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing imagining a background cache that computes during object construction be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny homework graph with tasks produce a dependency order for several displays. Include one ordinary case, one boundary and one deliberate failure caused by imagining a background cache that computes during object construction. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: A cached_property descriptor runs its getter only when normal lookup finds no same-named value in the instance dictionary. It shows a trace, not only a final value. The ordinary case should demonstrate “Construction prints only before; the first r.total lookup prints compute and returns 42.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for The descriptor computes on a missing attribute, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For The descriptor computes on a missing attribute, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 2 OF 20 . Build the model

2. The result is written into the instance

Back to contents

After a successful getter call, cached_property stores the returned value under the property name in the instance dictionary. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is looking for a hidden global cache owned by functools. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for The result is written into the instance, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the The result is written into the instance chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on looking for a hidden global cache owned by functools. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

r=Report()
print(r.__dict__)
_ = r.total
print(r.__dict__)

Explained result. The second dictionary contains total mapped to 42, making the cache visible and per instance. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: science dataset. Contrast the rule using measurements produce one expensive calibration summary. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “After a successful getter call, cached_property stores the returned value under the property name in the instance dictionary.” Apply this procedure: State the contract for The result is written into the instance, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The second dictionary contains total mapped to 42, making the cache visible and per instance. For the science dataset, add one near-miss that exposes looking for a hidden global cache owned by functools. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: CCA roster. Stress-test the rule using participants produce an index used repeatedly after construction. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “After a successful getter call, cached_property stores the returned value under the property name in the instance dictionary.” Apply this procedure: State the contract for The result is written into the instance, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The second dictionary contains total mapped to 42, making the cache visible and per instance. For the CCA roster, add one near-miss that exposes looking for a hidden global cache owned by functools. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: budget worksheet. Explain the rule using transactions produce a category total until source rows change. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “After a successful getter call, cached_property stores the returned value under the property name in the instance dictionary.” Apply this procedure: State the contract for The result is written into the instance, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The second dictionary contains total mapped to 42, making the cache visible and per instance. For the budget worksheet, add one near-miss that exposes looking for a hidden global cache owned by functools. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: homework graph. Transfer the rule using tasks produce a dependency order for several displays. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “After a successful getter call, cached_property stores the returned value under the property name in the instance dictionary.” Apply this procedure: State the contract for The result is written into the instance, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The second dictionary contains total mapped to 42, making the cache visible and per instance. For the homework graph, add one near-miss that exposes looking for a hidden global cache owned by functools. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers looking for a hidden global cache owned by functools.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for The result is written into the instance, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from The result is written into the instance?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing looking for a hidden global cache owned by functools be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny library catalogue with records produce a search index after first request. Include one ordinary case, one boundary and one deliberate failure caused by looking for a hidden global cache owned by functools. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: After a successful getter call, cached_property stores the returned value under the property name in the instance dictionary. It shows a trace, not only a final value. The ordinary case should demonstrate “The second dictionary contains total mapped to 42, making the cache visible and per instance.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for The result is written into the instance, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For The result is written into the instance, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 3 OF 20 . Build the model

3. Later reads use the stored value

Back to contents

Once the instance contains the same-named attribute, ordinary attribute lookup returns it without calling the descriptor getter again. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is expecting every read to revalidate source fields automatically. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Later reads use the stored value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the Later reads use the stored value chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on expecting every read to revalidate source fields automatically. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

r=Report()
print(r.total); print(r.total)

Explained result. The compute message appears once because the second lookup finds the stored instance value. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: budget worksheet. Stress-test the rule using transactions produce a category total until source rows change. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Once the instance contains the same-named attribute, ordinary attribute lookup returns it without calling the descriptor getter again.” Apply this procedure: State the contract for Later reads use the stored value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The compute message appears once because the second lookup finds the stored instance value. For the budget worksheet, add one near-miss that exposes expecting every read to revalidate source fields automatically. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: homework graph. Explain the rule using tasks produce a dependency order for several displays. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Once the instance contains the same-named attribute, ordinary attribute lookup returns it without calling the descriptor getter again.” Apply this procedure: State the contract for Later reads use the stored value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The compute message appears once because the second lookup finds the stored instance value. For the homework graph, add one near-miss that exposes expecting every read to revalidate source fields automatically. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: library catalogue. Transfer the rule using records produce a search index after first request. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Once the instance contains the same-named attribute, ordinary attribute lookup returns it without calling the descriptor getter again.” Apply this procedure: State the contract for Later reads use the stored value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The compute message appears once because the second lookup finds the stored instance value. For the library catalogue, add one near-miss that exposes expecting every read to revalidate source fields automatically. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: test laboratory. Predict the rule using call counts, deletion, mutation and threads reveal the cache lifecycle. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Once the instance contains the same-named attribute, ordinary attribute lookup returns it without calling the descriptor getter again.” Apply this procedure: State the contract for Later reads use the stored value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The compute message appears once because the second lookup finds the stored instance value. For the test laboratory, add one near-miss that exposes expecting every read to revalidate source fields automatically. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers expecting every read to revalidate source fields automatically.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for Later reads use the stored value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from Later reads use the stored value?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing expecting every read to revalidate source fields automatically be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny test laboratory with call counts, deletion, mutation and threads reveal the cache lifecycle. Include one ordinary case, one boundary and one deliberate failure caused by expecting every read to revalidate source fields automatically. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: Once the instance contains the same-named attribute, ordinary attribute lookup returns it without calling the descriptor getter again. It shows a trace, not only a final value. The ordinary case should demonstrate “The compute message appears once because the second lookup finds the stored instance value.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Later reads use the stored value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For Later reads use the stored value, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 4 OF 20 . Build the model

4. Writes can replace the cached value

Back to contents

Unlike an ordinary read-only property, cached_property permits assignment; the new instance value then takes precedence. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is assuming assignment must raise merely because decorator syntax resembles property. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Writes can replace the cached value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the Writes can replace the cached value chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on assuming assignment must raise merely because decorator syntax resembles property. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

r=Report(); _=r.total
r.total=99
print(r.total)

Explained result. The printed value is 99 and the getter is not called again for that read. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: library catalogue. Explain the rule using records produce a search index after first request. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Unlike an ordinary read-only property, cached_property permits assignment; the new instance value then takes precedence.” Apply this procedure: State the contract for Writes can replace the cached value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The printed value is 99 and the getter is not called again for that read. For the library catalogue, add one near-miss that exposes assuming assignment must raise merely because decorator syntax resembles property. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: test laboratory. Transfer the rule using call counts, deletion, mutation and threads reveal the cache lifecycle. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Unlike an ordinary read-only property, cached_property permits assignment; the new instance value then takes precedence.” Apply this procedure: State the contract for Writes can replace the cached value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The printed value is 99 and the getter is not called again for that read. For the test laboratory, add one near-miss that exposes assuming assignment must raise merely because decorator syntax resembles property. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: revision summary. Predict the rule using a large set of quiz attempts produces one derived mastery score. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Unlike an ordinary read-only property, cached_property permits assignment; the new instance value then takes precedence.” Apply this procedure: State the contract for Writes can replace the cached value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The printed value is 99 and the getter is not called again for that read. For the revision summary, add one near-miss that exposes assuming assignment must raise merely because decorator syntax resembles property. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: reading log. Contrast the rule using many entries produce a normalized total only when the report asks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Unlike an ordinary read-only property, cached_property permits assignment; the new instance value then takes precedence.” Apply this procedure: State the contract for Writes can replace the cached value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The printed value is 99 and the getter is not called again for that read. For the reading log, add one near-miss that exposes assuming assignment must raise merely because decorator syntax resembles property. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers assuming assignment must raise merely because decorator syntax resembles property.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for Writes can replace the cached value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from Writes can replace the cached value?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing assuming assignment must raise merely because decorator syntax resembles property be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny revision summary with a large set of quiz attempts produces one derived mastery score. Include one ordinary case, one boundary and one deliberate failure caused by assuming assignment must raise merely because decorator syntax resembles property. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: Unlike an ordinary read-only property, cached_property permits assignment; the new instance value then takes precedence. It shows a trace, not only a final value. The ordinary case should demonstrate “The printed value is 99 and the getter is not called again for that read.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Writes can replace the cached value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For Writes can replace the cached value, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 5 OF 20 . Use the core tools

5. Deletion clears the cache entry

Back to contents

Deleting the same-named instance attribute removes the cached value so a later lookup can invoke the getter again. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is adding a separate clear flag that the descriptor never consults. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Deletion clears the cache entry, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the Deletion clears the cache entry chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on adding a separate clear flag that the descriptor never consults. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

r=Report(); print(r.total)
del r.total
print(r.total)

Explained result. The getter runs once before deletion and once after deletion. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: revision summary. Transfer the rule using a large set of quiz attempts produces one derived mastery score. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Deleting the same-named instance attribute removes the cached value so a later lookup can invoke the getter again.” Apply this procedure: State the contract for Deletion clears the cache entry, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The getter runs once before deletion and once after deletion. For the revision summary, add one near-miss that exposes adding a separate clear flag that the descriptor never consults. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: reading log. Predict the rule using many entries produce a normalized total only when the report asks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Deleting the same-named instance attribute removes the cached value so a later lookup can invoke the getter again.” Apply this procedure: State the contract for Deletion clears the cache entry, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The getter runs once before deletion and once after deletion. For the reading log, add one near-miss that exposes adding a separate clear flag that the descriptor never consults. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: science dataset. Contrast the rule using measurements produce one expensive calibration summary. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Deleting the same-named instance attribute removes the cached value so a later lookup can invoke the getter again.” Apply this procedure: State the contract for Deletion clears the cache entry, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The getter runs once before deletion and once after deletion. For the science dataset, add one near-miss that exposes adding a separate clear flag that the descriptor never consults. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: CCA roster. Stress-test the rule using participants produce an index used repeatedly after construction. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Deleting the same-named instance attribute removes the cached value so a later lookup can invoke the getter again.” Apply this procedure: State the contract for Deletion clears the cache entry, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The getter runs once before deletion and once after deletion. For the CCA roster, add one near-miss that exposes adding a separate clear flag that the descriptor never consults. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers adding a separate clear flag that the descriptor never consults.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for Deletion clears the cache entry, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from Deletion clears the cache entry?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing adding a separate clear flag that the descriptor never consults be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny reading log with many entries produce a normalized total only when the report asks. Include one ordinary case, one boundary and one deliberate failure caused by adding a separate clear flag that the descriptor never consults. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: Deleting the same-named instance attribute removes the cached value so a later lookup can invoke the getter again. It shows a trace, not only a final value. The ordinary case should demonstrate “The getter runs once before deletion and once after deletion.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Deletion clears the cache entry, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For Deletion clears the cache entry, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 6 OF 20 . Use the core tools

6. Each instance has its own lifecycle

Back to contents

The value is stored in each object’s dictionary, so first access, replacement and deletion on one instance do not control another. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is treating one warmed instance as proof all instances are warm. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Each instance has its own lifecycle, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the Each instance has its own lifecycle chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on treating one warmed instance as proof all instances are warm. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

a=Report(); b=Report()
_=a.total
print(a.__dict__,b.__dict__)

Explained result. Only a contains total; b will compute on its own first access. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: science dataset. Predict the rule using measurements produce one expensive calibration summary. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “The value is stored in each object’s dictionary, so first access, replacement and deletion on one instance do not control another.” Apply this procedure: State the contract for Each instance has its own lifecycle, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Only a contains total; b will compute on its own first access. For the science dataset, add one near-miss that exposes treating one warmed instance as proof all instances are warm. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: CCA roster. Contrast the rule using participants produce an index used repeatedly after construction. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “The value is stored in each object’s dictionary, so first access, replacement and deletion on one instance do not control another.” Apply this procedure: State the contract for Each instance has its own lifecycle, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Only a contains total; b will compute on its own first access. For the CCA roster, add one near-miss that exposes treating one warmed instance as proof all instances are warm. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: budget worksheet. Stress-test the rule using transactions produce a category total until source rows change. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “The value is stored in each object’s dictionary, so first access, replacement and deletion on one instance do not control another.” Apply this procedure: State the contract for Each instance has its own lifecycle, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Only a contains total; b will compute on its own first access. For the budget worksheet, add one near-miss that exposes treating one warmed instance as proof all instances are warm. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: homework graph. Explain the rule using tasks produce a dependency order for several displays. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “The value is stored in each object’s dictionary, so first access, replacement and deletion on one instance do not control another.” Apply this procedure: State the contract for Each instance has its own lifecycle, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Only a contains total; b will compute on its own first access. For the homework graph, add one near-miss that exposes treating one warmed instance as proof all instances are warm. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers treating one warmed instance as proof all instances are warm.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for Each instance has its own lifecycle, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from Each instance has its own lifecycle?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing treating one warmed instance as proof all instances are warm be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny science dataset with measurements produce one expensive calibration summary. Include one ordinary case, one boundary and one deliberate failure caused by treating one warmed instance as proof all instances are warm. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: The value is stored in each object’s dictionary, so first access, replacement and deletion on one instance do not control another. It shows a trace, not only a final value. The ordinary case should demonstrate “Only a contains total; b will compute on its own first access.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Each instance has its own lifecycle, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For Each instance has its own lifecycle, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 7 OF 20 . Use the core tools

7. Returning None still creates a cache entry

Back to contents

A successful getter result of None is stored like any other value; cached_property does not use None as a missing sentinel. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is recomputing manually whenever the observed value is None. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Returning None still creates a cache entry, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the Returning None still creates a cache entry chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on recomputing manually whenever the observed value is None. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

class Maybe:
 @cached_property
 def value(self): return None
m=Maybe(); print(m.value,m.__dict__)

Explained result. The dictionary contains value: None, so later reads do not call the getter merely because the value is None. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: budget worksheet. Contrast the rule using transactions produce a category total until source rows change. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A successful getter result of None is stored like any other value; cached_property does not use None as a missing sentinel.” Apply this procedure: State the contract for Returning None still creates a cache entry, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The dictionary contains value: None, so later reads do not call the getter merely because the value is None. For the budget worksheet, add one near-miss that exposes recomputing manually whenever the observed value is None. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: homework graph. Stress-test the rule using tasks produce a dependency order for several displays. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A successful getter result of None is stored like any other value; cached_property does not use None as a missing sentinel.” Apply this procedure: State the contract for Returning None still creates a cache entry, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The dictionary contains value: None, so later reads do not call the getter merely because the value is None. For the homework graph, add one near-miss that exposes recomputing manually whenever the observed value is None. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: library catalogue. Explain the rule using records produce a search index after first request. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A successful getter result of None is stored like any other value; cached_property does not use None as a missing sentinel.” Apply this procedure: State the contract for Returning None still creates a cache entry, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The dictionary contains value: None, so later reads do not call the getter merely because the value is None. For the library catalogue, add one near-miss that exposes recomputing manually whenever the observed value is None. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: test laboratory. Transfer the rule using call counts, deletion, mutation and threads reveal the cache lifecycle. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A successful getter result of None is stored like any other value; cached_property does not use None as a missing sentinel.” Apply this procedure: State the contract for Returning None still creates a cache entry, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The dictionary contains value: None, so later reads do not call the getter merely because the value is None. For the test laboratory, add one near-miss that exposes recomputing manually whenever the observed value is None. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers recomputing manually whenever the observed value is None.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for Returning None still creates a cache entry, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from Returning None still creates a cache entry?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing recomputing manually whenever the observed value is None be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny CCA roster with participants produce an index used repeatedly after construction. Include one ordinary case, one boundary and one deliberate failure caused by recomputing manually whenever the observed value is None. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: A successful getter result of None is stored like any other value; cached_property does not use None as a missing sentinel. It shows a trace, not only a final value. The ordinary case should demonstrate “The dictionary contains value: None, so later reads do not call the getter merely because the value is None.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Returning None still creates a cache entry, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For Returning None still creates a cache entry, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 8 OF 20 . Use the core tools

8. Exceptions are not cached as values

Back to contents

If the getter raises, no successful result is written, so a later lookup attempts the getter again. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is assuming the first failure permanently poisons the property. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Exceptions are not cached as values, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the Exceptions are not cached as values chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on assuming the first failure permanently poisons the property. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

class Retry:
 def __init__(self): self.n=0
 @cached_property
 def value(self):
  self.n+=1
  if self.n==1: raise ValueError('first')
  return self.n

Explained result. After the first exception, value is absent; the next lookup runs again and returns 2. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: library catalogue. Stress-test the rule using records produce a search index after first request. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “If the getter raises, no successful result is written, so a later lookup attempts the getter again.” Apply this procedure: State the contract for Exceptions are not cached as values, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: After the first exception, value is absent; the next lookup runs again and returns 2. For the library catalogue, add one near-miss that exposes assuming the first failure permanently poisons the property. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: test laboratory. Explain the rule using call counts, deletion, mutation and threads reveal the cache lifecycle. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “If the getter raises, no successful result is written, so a later lookup attempts the getter again.” Apply this procedure: State the contract for Exceptions are not cached as values, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: After the first exception, value is absent; the next lookup runs again and returns 2. For the test laboratory, add one near-miss that exposes assuming the first failure permanently poisons the property. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: revision summary. Transfer the rule using a large set of quiz attempts produces one derived mastery score. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “If the getter raises, no successful result is written, so a later lookup attempts the getter again.” Apply this procedure: State the contract for Exceptions are not cached as values, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: After the first exception, value is absent; the next lookup runs again and returns 2. For the revision summary, add one near-miss that exposes assuming the first failure permanently poisons the property. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: reading log. Predict the rule using many entries produce a normalized total only when the report asks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “If the getter raises, no successful result is written, so a later lookup attempts the getter again.” Apply this procedure: State the contract for Exceptions are not cached as values, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: After the first exception, value is absent; the next lookup runs again and returns 2. For the reading log, add one near-miss that exposes assuming the first failure permanently poisons the property. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers assuming the first failure permanently poisons the property.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for Exceptions are not cached as values, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from Exceptions are not cached as values?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing assuming the first failure permanently poisons the property be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny budget worksheet with transactions produce a category total until source rows change. Include one ordinary case, one boundary and one deliberate failure caused by assuming the first failure permanently poisons the property. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: If the getter raises, no successful result is written, so a later lookup attempts the getter again. It shows a trace, not only a final value. The ordinary case should demonstrate “After the first exception, value is absent; the next lookup runs again and returns 2.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Exceptions are not cached as values, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For Exceptions are not cached as values, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 9 OF 20 . Handle boundaries

9. The getter receives the instance

Back to contents

The decorated function is an instance method and can derive its result from stable instance state available at first access. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is writing a zero-argument helper and forgetting descriptor method binding. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for The getter receives the instance, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the The getter receives the instance chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on writing a zero-argument helper and forgetting descriptor method binding. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

class Rectangle:
 def __init__(self,w,h): self.w=w; self.h=h
 @cached_property
 def area(self): return self.w*self.h

Explained result. Rectangle(3,4).area calls area with that rectangle as self and stores 12 on it. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: revision summary. Explain the rule using a large set of quiz attempts produces one derived mastery score. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “The decorated function is an instance method and can derive its result from stable instance state available at first access.” Apply this procedure: State the contract for The getter receives the instance, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Rectangle(3,4).area calls area with that rectangle as self and stores 12 on it. For the revision summary, add one near-miss that exposes writing a zero-argument helper and forgetting descriptor method binding. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: reading log. Transfer the rule using many entries produce a normalized total only when the report asks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “The decorated function is an instance method and can derive its result from stable instance state available at first access.” Apply this procedure: State the contract for The getter receives the instance, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Rectangle(3,4).area calls area with that rectangle as self and stores 12 on it. For the reading log, add one near-miss that exposes writing a zero-argument helper and forgetting descriptor method binding. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: science dataset. Predict the rule using measurements produce one expensive calibration summary. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “The decorated function is an instance method and can derive its result from stable instance state available at first access.” Apply this procedure: State the contract for The getter receives the instance, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Rectangle(3,4).area calls area with that rectangle as self and stores 12 on it. For the science dataset, add one near-miss that exposes writing a zero-argument helper and forgetting descriptor method binding. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: CCA roster. Contrast the rule using participants produce an index used repeatedly after construction. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “The decorated function is an instance method and can derive its result from stable instance state available at first access.” Apply this procedure: State the contract for The getter receives the instance, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Rectangle(3,4).area calls area with that rectangle as self and stores 12 on it. For the CCA roster, add one near-miss that exposes writing a zero-argument helper and forgetting descriptor method binding. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers writing a zero-argument helper and forgetting descriptor method binding.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for The getter receives the instance, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from The getter receives the instance?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing writing a zero-argument helper and forgetting descriptor method binding be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny homework graph with tasks produce a dependency order for several displays. Include one ordinary case, one boundary and one deliberate failure caused by writing a zero-argument helper and forgetting descriptor method binding. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: The decorated function is an instance method and can derive its result from stable instance state available at first access. It shows a trace, not only a final value. The ordinary case should demonstrate “Rectangle(3,4).area calls area with that rectangle as self and stores 12 on it.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for The getter receives the instance, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For The getter receives the instance, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 10 OF 20 . Handle boundaries

10. Mutable source fields require invalidation policy

Back to contents

Changing inputs after the first read does not invalidate the stored value; the application must delete, recompute or prevent the mutation. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is calling a stale result a decorator bug when no dependency tracking was defined. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Mutable source fields require invalidation policy, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the Mutable source fields require invalidation policy chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on calling a stale result a decorator bug when no dependency tracking was defined. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

r=Rectangle(3,4); print(r.area)
r.w=10; print(r.area)
del r.area; print(r.area)

Explained result. The reads produce 12, 12 and then 40 because only deletion reopens computation. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: science dataset. Transfer the rule using measurements produce one expensive calibration summary. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Changing inputs after the first read does not invalidate the stored value; the application must delete, recompute or prevent the mutation.” Apply this procedure: State the contract for Mutable source fields require invalidation policy, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The reads produce 12, 12 and then 40 because only deletion reopens computation. For the science dataset, add one near-miss that exposes calling a stale result a decorator bug when no dependency tracking was defined. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: CCA roster. Predict the rule using participants produce an index used repeatedly after construction. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Changing inputs after the first read does not invalidate the stored value; the application must delete, recompute or prevent the mutation.” Apply this procedure: State the contract for Mutable source fields require invalidation policy, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The reads produce 12, 12 and then 40 because only deletion reopens computation. For the CCA roster, add one near-miss that exposes calling a stale result a decorator bug when no dependency tracking was defined. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: budget worksheet. Contrast the rule using transactions produce a category total until source rows change. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Changing inputs after the first read does not invalidate the stored value; the application must delete, recompute or prevent the mutation.” Apply this procedure: State the contract for Mutable source fields require invalidation policy, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The reads produce 12, 12 and then 40 because only deletion reopens computation. For the budget worksheet, add one near-miss that exposes calling a stale result a decorator bug when no dependency tracking was defined. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: homework graph. Stress-test the rule using tasks produce a dependency order for several displays. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Changing inputs after the first read does not invalidate the stored value; the application must delete, recompute or prevent the mutation.” Apply this procedure: State the contract for Mutable source fields require invalidation policy, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The reads produce 12, 12 and then 40 because only deletion reopens computation. For the homework graph, add one near-miss that exposes calling a stale result a decorator bug when no dependency tracking was defined. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers calling a stale result a decorator bug when no dependency tracking was defined.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for Mutable source fields require invalidation policy, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from Mutable source fields require invalidation policy?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing calling a stale result a decorator bug when no dependency tracking was defined be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny library catalogue with records produce a search index after first request. Include one ordinary case, one boundary and one deliberate failure caused by calling a stale result a decorator bug when no dependency tracking was defined. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: Changing inputs after the first read does not invalidate the stored value; the application must delete, recompute or prevent the mutation. It shows a trace, not only a final value. The ordinary case should demonstrate “The reads produce 12, 12 and then 40 because only deletion reopens computation.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Mutable source fields require invalidation policy, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For Mutable source fields require invalidation policy, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 11 OF 20 . Handle boundaries

11. A mutable instance dictionary is required

Back to contents

cached_property needs an instance __dict__ that is a mutable mapping so it can write the result under the attribute name. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is applying the decorator to every object regardless of storage model. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for A mutable instance dictionary is required, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the A mutable instance dictionary is required chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on applying the decorator to every object regardless of storage model. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

class Slotted:
 __slots__=('x',)
 @cached_property
 def doubled(self): return self.x*2

Explained result. An instance without __dict__ cannot accept the cache write and access raises TypeError. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: budget worksheet. Predict the rule using transactions produce a category total until source rows change. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “cached_property needs an instance __dict__ that is a mutable mapping so it can write the result under the attribute name.” Apply this procedure: State the contract for A mutable instance dictionary is required, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: An instance without __dict__ cannot accept the cache write and access raises TypeError. For the budget worksheet, add one near-miss that exposes applying the decorator to every object regardless of storage model. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: homework graph. Contrast the rule using tasks produce a dependency order for several displays. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “cached_property needs an instance __dict__ that is a mutable mapping so it can write the result under the attribute name.” Apply this procedure: State the contract for A mutable instance dictionary is required, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: An instance without __dict__ cannot accept the cache write and access raises TypeError. For the homework graph, add one near-miss that exposes applying the decorator to every object regardless of storage model. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: library catalogue. Stress-test the rule using records produce a search index after first request. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “cached_property needs an instance __dict__ that is a mutable mapping so it can write the result under the attribute name.” Apply this procedure: State the contract for A mutable instance dictionary is required, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: An instance without __dict__ cannot accept the cache write and access raises TypeError. For the library catalogue, add one near-miss that exposes applying the decorator to every object regardless of storage model. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: test laboratory. Explain the rule using call counts, deletion, mutation and threads reveal the cache lifecycle. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “cached_property needs an instance __dict__ that is a mutable mapping so it can write the result under the attribute name.” Apply this procedure: State the contract for A mutable instance dictionary is required, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: An instance without __dict__ cannot accept the cache write and access raises TypeError. For the test laboratory, add one near-miss that exposes applying the decorator to every object regardless of storage model. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers applying the decorator to every object regardless of storage model.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for A mutable instance dictionary is required, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from A mutable instance dictionary is required?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing applying the decorator to every object regardless of storage model be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny test laboratory with call counts, deletion, mutation and threads reveal the cache lifecycle. Include one ordinary case, one boundary and one deliberate failure caused by applying the decorator to every object regardless of storage model. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: cached_property needs an instance __dict__ that is a mutable mapping so it can write the result under the attribute name. It shows a trace, not only a final value. The ordinary case should demonstrate “An instance without __dict__ cannot accept the cache write and access raises TypeError.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for A mutable instance dictionary is required, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For A mutable instance dictionary is required, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 12 OF 20 . Handle boundaries

12. Slots can include a dictionary deliberately

Back to contents

A slotted class can support cached_property when __dict__ is included among its slots, accepting the memory and flexibility trade-off. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is adding slots for strict storage and then silently restoring all dynamic attributes. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Slots can include a dictionary deliberately, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the Slots can include a dictionary deliberately chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on adding slots for strict storage and then silently restoring all dynamic attributes. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

class Supported:
 __slots__=('x','__dict__')
 @cached_property
 def doubled(self): return self.x*2

Explained result. The declared dictionary gives the descriptor somewhere to store doubled, but changes the class storage contract. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: library catalogue. Contrast the rule using records produce a search index after first request. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A slotted class can support cached_property when __dict__ is included among its slots, accepting the memory and flexibility trade-off.” Apply this procedure: State the contract for Slots can include a dictionary deliberately, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The declared dictionary gives the descriptor somewhere to store doubled, but changes the class storage contract. For the library catalogue, add one near-miss that exposes adding slots for strict storage and then silently restoring all dynamic attributes. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: test laboratory. Stress-test the rule using call counts, deletion, mutation and threads reveal the cache lifecycle. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A slotted class can support cached_property when __dict__ is included among its slots, accepting the memory and flexibility trade-off.” Apply this procedure: State the contract for Slots can include a dictionary deliberately, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The declared dictionary gives the descriptor somewhere to store doubled, but changes the class storage contract. For the test laboratory, add one near-miss that exposes adding slots for strict storage and then silently restoring all dynamic attributes. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: revision summary. Explain the rule using a large set of quiz attempts produces one derived mastery score. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A slotted class can support cached_property when __dict__ is included among its slots, accepting the memory and flexibility trade-off.” Apply this procedure: State the contract for Slots can include a dictionary deliberately, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The declared dictionary gives the descriptor somewhere to store doubled, but changes the class storage contract. For the revision summary, add one near-miss that exposes adding slots for strict storage and then silently restoring all dynamic attributes. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: reading log. Transfer the rule using many entries produce a normalized total only when the report asks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A slotted class can support cached_property when __dict__ is included among its slots, accepting the memory and flexibility trade-off.” Apply this procedure: State the contract for Slots can include a dictionary deliberately, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The declared dictionary gives the descriptor somewhere to store doubled, but changes the class storage contract. For the reading log, add one near-miss that exposes adding slots for strict storage and then silently restoring all dynamic attributes. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers adding slots for strict storage and then silently restoring all dynamic attributes.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for Slots can include a dictionary deliberately, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from Slots can include a dictionary deliberately?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing adding slots for strict storage and then silently restoring all dynamic attributes be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny revision summary with a large set of quiz attempts produces one derived mastery score. Include one ordinary case, one boundary and one deliberate failure caused by adding slots for strict storage and then silently restoring all dynamic attributes. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: A slotted class can support cached_property when __dict__ is included among its slots, accepting the memory and flexibility trade-off. It shows a trace, not only a final value. The ordinary case should demonstrate “The declared dictionary gives the descriptor somewhere to store doubled, but changes the class storage contract.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Slots can include a dictionary deliberately, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For Slots can include a dictionary deliberately, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 13 OF 20 . Debug and verify

13. Metaclass instances expose read-only namespace proxies

Back to contents

A type object’s __dict__ is a read-only mapping proxy, so cached_property is not a general cache for metaclass attributes. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is assuming every object with a visible __dict__ has a writable dictionary. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Metaclass instances expose read-only namespace proxies, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the Metaclass instances expose read-only namespace proxies chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on assuming every object with a visible __dict__ has a writable dictionary. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

class Meta(type):
 @cached_property
 def label(cls): return cls.__name__.lower()

Explained result. Access cannot store label through the read-only class namespace proxy; a different metaclass-level design is required. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: revision summary. Stress-test the rule using a large set of quiz attempts produces one derived mastery score. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A type object’s __dict__ is a read-only mapping proxy, so cached_property is not a general cache for metaclass attributes.” Apply this procedure: State the contract for Metaclass instances expose read-only namespace proxies, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Access cannot store label through the read-only class namespace proxy; a different metaclass-level design is required. For the revision summary, add one near-miss that exposes assuming every object with a visible __dict__ has a writable dictionary. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: reading log. Explain the rule using many entries produce a normalized total only when the report asks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A type object’s __dict__ is a read-only mapping proxy, so cached_property is not a general cache for metaclass attributes.” Apply this procedure: State the contract for Metaclass instances expose read-only namespace proxies, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Access cannot store label through the read-only class namespace proxy; a different metaclass-level design is required. For the reading log, add one near-miss that exposes assuming every object with a visible __dict__ has a writable dictionary. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: science dataset. Transfer the rule using measurements produce one expensive calibration summary. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A type object’s __dict__ is a read-only mapping proxy, so cached_property is not a general cache for metaclass attributes.” Apply this procedure: State the contract for Metaclass instances expose read-only namespace proxies, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Access cannot store label through the read-only class namespace proxy; a different metaclass-level design is required. For the science dataset, add one near-miss that exposes assuming every object with a visible __dict__ has a writable dictionary. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: CCA roster. Predict the rule using participants produce an index used repeatedly after construction. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A type object’s __dict__ is a read-only mapping proxy, so cached_property is not a general cache for metaclass attributes.” Apply this procedure: State the contract for Metaclass instances expose read-only namespace proxies, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Access cannot store label through the read-only class namespace proxy; a different metaclass-level design is required. For the CCA roster, add one near-miss that exposes assuming every object with a visible __dict__ has a writable dictionary. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers assuming every object with a visible __dict__ has a writable dictionary.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for Metaclass instances expose read-only namespace proxies, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from Metaclass instances expose read-only namespace proxies?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing assuming every object with a visible __dict__ has a writable dictionary be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny reading log with many entries produce a normalized total only when the report asks. Include one ordinary case, one boundary and one deliberate failure caused by assuming every object with a visible __dict__ has a writable dictionary. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: A type object’s __dict__ is a read-only mapping proxy, so cached_property is not a general cache for metaclass attributes. It shows a trace, not only a final value. The ordinary case should demonstrate “Access cannot store label through the read-only class namespace proxy; a different metaclass-level design is required.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Metaclass instances expose read-only namespace proxies, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For Metaclass instances expose read-only namespace proxies, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 14 OF 20 . Debug and verify

14. Python 3.12 removed the per-property lock

Back to contents

Current cached_property does not serialize concurrent first access, so the getter may run more than once on the same instance. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is relying on an undocumented lock from older Python behaviour. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Python 3.12 removed the per-property lock, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the Python 3.12 removed the per-property lock chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on relying on an undocumented lock from older Python behaviour. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

# Two threads may both observe the missing key and run the getter.
# Protect the getter or surrounding access when once-only work matters.

Explained result. The latest completed getter writes the cached value; duplicate first computations remain possible. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: science dataset. Explain the rule using measurements produce one expensive calibration summary. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Current cached_property does not serialize concurrent first access, so the getter may run more than once on the same instance.” Apply this procedure: State the contract for Python 3.12 removed the per-property lock, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The latest completed getter writes the cached value; duplicate first computations remain possible. For the science dataset, add one near-miss that exposes relying on an undocumented lock from older Python behaviour. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: CCA roster. Transfer the rule using participants produce an index used repeatedly after construction. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Current cached_property does not serialize concurrent first access, so the getter may run more than once on the same instance.” Apply this procedure: State the contract for Python 3.12 removed the per-property lock, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The latest completed getter writes the cached value; duplicate first computations remain possible. For the CCA roster, add one near-miss that exposes relying on an undocumented lock from older Python behaviour. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: budget worksheet. Predict the rule using transactions produce a category total until source rows change. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Current cached_property does not serialize concurrent first access, so the getter may run more than once on the same instance.” Apply this procedure: State the contract for Python 3.12 removed the per-property lock, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The latest completed getter writes the cached value; duplicate first computations remain possible. For the budget worksheet, add one near-miss that exposes relying on an undocumented lock from older Python behaviour. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: homework graph. Contrast the rule using tasks produce a dependency order for several displays. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Current cached_property does not serialize concurrent first access, so the getter may run more than once on the same instance.” Apply this procedure: State the contract for Python 3.12 removed the per-property lock, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The latest completed getter writes the cached value; duplicate first computations remain possible. For the homework graph, add one near-miss that exposes relying on an undocumented lock from older Python behaviour. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers relying on an undocumented lock from older Python behaviour.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for Python 3.12 removed the per-property lock, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from Python 3.12 removed the per-property lock?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing relying on an undocumented lock from older Python behaviour be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny science dataset with measurements produce one expensive calibration summary. Include one ordinary case, one boundary and one deliberate failure caused by relying on an undocumented lock from older Python behaviour. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: Current cached_property does not serialize concurrent first access, so the getter may run more than once on the same instance. It shows a trace, not only a final value. The ordinary case should demonstrate “The latest completed getter writes the cached value; duplicate first computations remain possible.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Python 3.12 removed the per-property lock, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For Python 3.12 removed the per-property lock, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 15 OF 20 . Debug and verify

15. Idempotent getters make races safer

Back to contents

A getter whose repeated execution has the same harmless effect tolerates concurrent first reads better than one that sends mail or charges money. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is placing an irreversible side effect inside a getter and calling it exactly once. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Idempotent getters make races safer, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the Idempotent getters make races safer chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on placing an irreversible side effect inside a getter and calling it exactly once. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

@cached_property
def normalized(self):
    return tuple(sorted(self.raw))

Explained result. Repeated concurrent computation can waste work but produces the same immutable value without repeating an external side effect. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: budget worksheet. Transfer the rule using transactions produce a category total until source rows change. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A getter whose repeated execution has the same harmless effect tolerates concurrent first reads better than one that sends mail or charges money.” Apply this procedure: State the contract for Idempotent getters make races safer, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Repeated concurrent computation can waste work but produces the same immutable value without repeating an external side effect. For the budget worksheet, add one near-miss that exposes placing an irreversible side effect inside a getter and calling it exactly once. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: homework graph. Predict the rule using tasks produce a dependency order for several displays. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A getter whose repeated execution has the same harmless effect tolerates concurrent first reads better than one that sends mail or charges money.” Apply this procedure: State the contract for Idempotent getters make races safer, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Repeated concurrent computation can waste work but produces the same immutable value without repeating an external side effect. For the homework graph, add one near-miss that exposes placing an irreversible side effect inside a getter and calling it exactly once. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: library catalogue. Contrast the rule using records produce a search index after first request. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A getter whose repeated execution has the same harmless effect tolerates concurrent first reads better than one that sends mail or charges money.” Apply this procedure: State the contract for Idempotent getters make races safer, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Repeated concurrent computation can waste work but produces the same immutable value without repeating an external side effect. For the library catalogue, add one near-miss that exposes placing an irreversible side effect inside a getter and calling it exactly once. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: test laboratory. Stress-test the rule using call counts, deletion, mutation and threads reveal the cache lifecycle. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A getter whose repeated execution has the same harmless effect tolerates concurrent first reads better than one that sends mail or charges money.” Apply this procedure: State the contract for Idempotent getters make races safer, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: Repeated concurrent computation can waste work but produces the same immutable value without repeating an external side effect. For the test laboratory, add one near-miss that exposes placing an irreversible side effect inside a getter and calling it exactly once. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers placing an irreversible side effect inside a getter and calling it exactly once.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for Idempotent getters make races safer, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from Idempotent getters make races safer?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing placing an irreversible side effect inside a getter and calling it exactly once be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny CCA roster with participants produce an index used repeatedly after construction. Include one ordinary case, one boundary and one deliberate failure caused by placing an irreversible side effect inside a getter and calling it exactly once. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: A getter whose repeated execution has the same harmless effect tolerates concurrent first reads better than one that sends mail or charges money. It shows a trace, not only a final value. The ordinary case should demonstrate “Repeated concurrent computation can waste work but produces the same immutable value without repeating an external side effect.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Idempotent getters make races safer, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For Idempotent getters make races safer, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 16 OF 20 . Debug and verify

16. Synchronize the critical work when needed

Back to contents

If duplicate execution is harmful, locking belongs inside the getter or around access with a second check appropriate to the application. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is adding one shared lock to unrelated objects and creating unnecessary contention. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Synchronize the critical work when needed, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the Synchronize the critical work when needed chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on adding one shared lock to unrelated objects and creating unnecessary contention. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

with self._lock:
    # perform the once-only protected calculation
    return build_value(self.source)

Explained result. The application, not cached_property, owns the required synchronization and side-effect policy. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: library catalogue. Predict the rule using records produce a search index after first request. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “If duplicate execution is harmful, locking belongs inside the getter or around access with a second check appropriate to the application.” Apply this procedure: State the contract for Synchronize the critical work when needed, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The application, not cached_property, owns the required synchronization and side-effect policy. For the library catalogue, add one near-miss that exposes adding one shared lock to unrelated objects and creating unnecessary contention. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: test laboratory. Contrast the rule using call counts, deletion, mutation and threads reveal the cache lifecycle. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “If duplicate execution is harmful, locking belongs inside the getter or around access with a second check appropriate to the application.” Apply this procedure: State the contract for Synchronize the critical work when needed, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The application, not cached_property, owns the required synchronization and side-effect policy. For the test laboratory, add one near-miss that exposes adding one shared lock to unrelated objects and creating unnecessary contention. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: revision summary. Stress-test the rule using a large set of quiz attempts produces one derived mastery score. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “If duplicate execution is harmful, locking belongs inside the getter or around access with a second check appropriate to the application.” Apply this procedure: State the contract for Synchronize the critical work when needed, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The application, not cached_property, owns the required synchronization and side-effect policy. For the revision summary, add one near-miss that exposes adding one shared lock to unrelated objects and creating unnecessary contention. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: reading log. Explain the rule using many entries produce a normalized total only when the report asks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “If duplicate execution is harmful, locking belongs inside the getter or around access with a second check appropriate to the application.” Apply this procedure: State the contract for Synchronize the critical work when needed, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The application, not cached_property, owns the required synchronization and side-effect policy. For the reading log, add one near-miss that exposes adding one shared lock to unrelated objects and creating unnecessary contention. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers adding one shared lock to unrelated objects and creating unnecessary contention.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for Synchronize the critical work when needed, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from Synchronize the critical work when needed?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing adding one shared lock to unrelated objects and creating unnecessary contention be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny budget worksheet with transactions produce a category total until source rows change. Include one ordinary case, one boundary and one deliberate failure caused by adding one shared lock to unrelated objects and creating unnecessary contention. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: If duplicate execution is harmful, locking belongs inside the getter or around access with a second check appropriate to the application. It shows a trace, not only a final value. The ordinary case should demonstrate “The application, not cached_property, owns the required synchronization and side-effect policy.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Synchronize the critical work when needed, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For Synchronize the critical work when needed, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 17 OF 20 . Transfer with judgment

17. Instance dictionaries have memory consequences

Back to contents

Writing the cached name can interfere with key-sharing dictionaries and adds per-instance state, so many lightly used objects deserve measurement. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is adding dozens of cached properties to millions of objects based only on speed intuition. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Instance dictionaries have memory consequences, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the Instance dictionaries have memory consequences chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on adding dozens of cached properties to millions of objects based only on speed intuition. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

import sys
before=sys.getsizeof(obj.__dict__)
_=obj.expensive
after=sys.getsizeof(obj.__dict__)

Explained result. A measurement can reveal storage change, but workload-level memory and hit rates decide whether the trade-off is worthwhile. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: revision summary. Contrast the rule using a large set of quiz attempts produces one derived mastery score. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Writing the cached name can interfere with key-sharing dictionaries and adds per-instance state, so many lightly used objects deserve measurement.” Apply this procedure: State the contract for Instance dictionaries have memory consequences, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: A measurement can reveal storage change, but workload-level memory and hit rates decide whether the trade-off is worthwhile. For the revision summary, add one near-miss that exposes adding dozens of cached properties to millions of objects based only on speed intuition. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: reading log. Stress-test the rule using many entries produce a normalized total only when the report asks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Writing the cached name can interfere with key-sharing dictionaries and adds per-instance state, so many lightly used objects deserve measurement.” Apply this procedure: State the contract for Instance dictionaries have memory consequences, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: A measurement can reveal storage change, but workload-level memory and hit rates decide whether the trade-off is worthwhile. For the reading log, add one near-miss that exposes adding dozens of cached properties to millions of objects based only on speed intuition. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: science dataset. Explain the rule using measurements produce one expensive calibration summary. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Writing the cached name can interfere with key-sharing dictionaries and adds per-instance state, so many lightly used objects deserve measurement.” Apply this procedure: State the contract for Instance dictionaries have memory consequences, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: A measurement can reveal storage change, but workload-level memory and hit rates decide whether the trade-off is worthwhile. For the science dataset, add one near-miss that exposes adding dozens of cached properties to millions of objects based only on speed intuition. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: CCA roster. Transfer the rule using participants produce an index used repeatedly after construction. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Writing the cached name can interfere with key-sharing dictionaries and adds per-instance state, so many lightly used objects deserve measurement.” Apply this procedure: State the contract for Instance dictionaries have memory consequences, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: A measurement can reveal storage change, but workload-level memory and hit rates decide whether the trade-off is worthwhile. For the CCA roster, add one near-miss that exposes adding dozens of cached properties to millions of objects based only on speed intuition. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers adding dozens of cached properties to millions of objects based only on speed intuition.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for Instance dictionaries have memory consequences, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from Instance dictionaries have memory consequences?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing adding dozens of cached properties to millions of objects based only on speed intuition be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny homework graph with tasks produce a dependency order for several displays. Include one ordinary case, one boundary and one deliberate failure caused by adding dozens of cached properties to millions of objects based only on speed intuition. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: Writing the cached name can interfere with key-sharing dictionaries and adds per-instance state, so many lightly used objects deserve measurement. It shows a trace, not only a final value. The ordinary case should demonstrate “A measurement can reveal storage change, but workload-level memory and hit rates decide whether the trade-off is worthwhile.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Instance dictionaries have memory consequences, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For Instance dictionaries have memory consequences, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 18 OF 20 . Transfer with judgment

18. Tests should assert calls and state transitions

Back to contents

A strong test observes the getter call count, stored dictionary key, replacement, deletion and recomputation rather than checking only one value. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is mocking the final result so heavily that the cache lifecycle is never exercised. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Tests should assert calls and state transitions, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the Tests should assert calls and state transitions chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on mocking the final result so heavily that the cache lifecycle is never exercised. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

r=CountingReport()
assert r.total==42 and r.total==42
assert r.calls==1
del r.total
assert r.total==42 and r.calls==2

Explained result. The assertions prove both the value and the documented transition from missing to stored to deleted to recomputed. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: science dataset. Stress-test the rule using measurements produce one expensive calibration summary. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A strong test observes the getter call count, stored dictionary key, replacement, deletion and recomputation rather than checking only one value.” Apply this procedure: State the contract for Tests should assert calls and state transitions, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The assertions prove both the value and the documented transition from missing to stored to deleted to recomputed. For the science dataset, add one near-miss that exposes mocking the final result so heavily that the cache lifecycle is never exercised. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: CCA roster. Explain the rule using participants produce an index used repeatedly after construction. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A strong test observes the getter call count, stored dictionary key, replacement, deletion and recomputation rather than checking only one value.” Apply this procedure: State the contract for Tests should assert calls and state transitions, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The assertions prove both the value and the documented transition from missing to stored to deleted to recomputed. For the CCA roster, add one near-miss that exposes mocking the final result so heavily that the cache lifecycle is never exercised. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: budget worksheet. Transfer the rule using transactions produce a category total until source rows change. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A strong test observes the getter call count, stored dictionary key, replacement, deletion and recomputation rather than checking only one value.” Apply this procedure: State the contract for Tests should assert calls and state transitions, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The assertions prove both the value and the documented transition from missing to stored to deleted to recomputed. For the budget worksheet, add one near-miss that exposes mocking the final result so heavily that the cache lifecycle is never exercised. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: homework graph. Predict the rule using tasks produce a dependency order for several displays. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “A strong test observes the getter call count, stored dictionary key, replacement, deletion and recomputation rather than checking only one value.” Apply this procedure: State the contract for Tests should assert calls and state transitions, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The assertions prove both the value and the documented transition from missing to stored to deleted to recomputed. For the homework graph, add one near-miss that exposes mocking the final result so heavily that the cache lifecycle is never exercised. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers mocking the final result so heavily that the cache lifecycle is never exercised.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for Tests should assert calls and state transitions, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from Tests should assert calls and state transitions?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing mocking the final result so heavily that the cache lifecycle is never exercised be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny library catalogue with records produce a search index after first request. Include one ordinary case, one boundary and one deliberate failure caused by mocking the final result so heavily that the cache lifecycle is never exercised. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: A strong test observes the getter call count, stored dictionary key, replacement, deletion and recomputation rather than checking only one value. It shows a trace, not only a final value. The ordinary case should demonstrate “The assertions prove both the value and the documented transition from missing to stored to deleted to recomputed.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Tests should assert calls and state transitions, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For Tests should assert calls and state transitions, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 19 OF 20 . Transfer with judgment

19. Explicit caches can model arguments and invalidation

Back to contents

lru_cache, a private sentinel, a versioned key or an explicit recompute method can be clearer when results depend on arguments or several changing sources. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is forcing every cache problem into a no-argument attribute interface. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Explicit caches can model arguments and invalidation, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the Explicit caches can model arguments and invalidation chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on forcing every cache problem into a no-argument attribute interface. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

def recompute_total(self):
    self._total=calculate(self.rows)
    self._version+=1

Explained result. The explicit method makes mutation, ownership and refresh visible when automatic attribute caching would hide them. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: budget worksheet. Explain the rule using transactions produce a category total until source rows change. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “lru_cache, a private sentinel, a versioned key or an explicit recompute method can be clearer when results depend on arguments or several changing sources.” Apply this procedure: State the contract for Explicit caches can model arguments and invalidation, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The explicit method makes mutation, ownership and refresh visible when automatic attribute caching would hide them. For the budget worksheet, add one near-miss that exposes forcing every cache problem into a no-argument attribute interface. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: homework graph. Transfer the rule using tasks produce a dependency order for several displays. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “lru_cache, a private sentinel, a versioned key or an explicit recompute method can be clearer when results depend on arguments or several changing sources.” Apply this procedure: State the contract for Explicit caches can model arguments and invalidation, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The explicit method makes mutation, ownership and refresh visible when automatic attribute caching would hide them. For the homework graph, add one near-miss that exposes forcing every cache problem into a no-argument attribute interface. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: library catalogue. Predict the rule using records produce a search index after first request. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “lru_cache, a private sentinel, a versioned key or an explicit recompute method can be clearer when results depend on arguments or several changing sources.” Apply this procedure: State the contract for Explicit caches can model arguments and invalidation, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The explicit method makes mutation, ownership and refresh visible when automatic attribute caching would hide them. For the library catalogue, add one near-miss that exposes forcing every cache problem into a no-argument attribute interface. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: test laboratory. Contrast the rule using call counts, deletion, mutation and threads reveal the cache lifecycle. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “lru_cache, a private sentinel, a versioned key or an explicit recompute method can be clearer when results depend on arguments or several changing sources.” Apply this procedure: State the contract for Explicit caches can model arguments and invalidation, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The explicit method makes mutation, ownership and refresh visible when automatic attribute caching would hide them. For the test laboratory, add one near-miss that exposes forcing every cache problem into a no-argument attribute interface. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers forcing every cache problem into a no-argument attribute interface.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for Explicit caches can model arguments and invalidation, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from Explicit caches can model arguments and invalidation?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing forcing every cache problem into a no-argument attribute interface be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny test laboratory with call counts, deletion, mutation and threads reveal the cache lifecycle. Include one ordinary case, one boundary and one deliberate failure caused by forcing every cache problem into a no-argument attribute interface. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: lru_cache, a private sentinel, a versioned key or an explicit recompute method can be clearer when results depend on arguments or several changing sources. It shows a trace, not only a final value. The ordinary case should demonstrate “The explicit method makes mutation, ownership and refresh visible when automatic attribute caching would hide them.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Explicit caches can model arguments and invalidation, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For Explicit caches can model arguments and invalidation, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

CHAPTER 20 OF 20 . Transfer with judgment

20. Choose cached_property only for a stable derived attribute

Back to contents

Use cached_property when an expensive instance-derived value is read like an attribute and remains valid until deliberate deletion; otherwise prefer property, a method or an explicit cache. Treat that sentence as a testable model. A secure learner can point to the relevant input, name the operation, describe the resulting state and identify one observation that would prove the model incomplete.

The high-value mistake in this chapter is decorating cheap, volatile or side-effecting work because caching sounds faster. It matters because the output may look reasonable while the ownership, ordering, identity or safety rule is wrong. State the contract for Choose cached_property only for a stable derived attribute, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.

For the Choose cached_property only for a stable derived attribute chapter on Python functools.cached_property, use a two-column trace during a short Punggol home session. On the left, write the predicted state for this exact mechanism before the tool runs. On the right, record the observation that bears on decorating cheap, volatile or side-effecting work because caching sounds faster. Explain the earliest difference with one causal sentence, then repeat only the smallest changed case.

Core worked example

# Stable and attribute-like: report.index
# Volatile or policy-driven: report.build_index(options)

Explained result. The interface communicates lifecycle: cached_property promises reuse of one stored instance value, while other forms expose recomputation or parameters. Check the boundary as well as the happy path: ask what happens with an empty input, a duplicate or tied value, an unsupported type, a missing path, a NULL, or a second reference to the same object. Only the relevant boundary should be kept; the list is a prompt for judgment, not a demand to force every case into every example.

Four purposeful transfer cases

Case 1: library catalogue. Transfer the rule using records produce a search index after first request. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Use cached_property when an expensive instance-derived value is read like an attribute and remains valid until deliberate deletion; otherwise prefer property, a method or an explicit cache.” Apply this procedure: State the contract for Choose cached_property only for a stable derived attribute, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The interface communicates lifecycle: cached_property promises reuse of one stored instance value, while other forms expose recomputation or parameters. For the library catalogue, add one near-miss that exposes decorating cheap, volatile or side-effecting work because caching sounds faster. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 2: test laboratory. Predict the rule using call counts, deletion, mutation and threads reveal the cache lifecycle. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Use cached_property when an expensive instance-derived value is read like an attribute and remains valid until deliberate deletion; otherwise prefer property, a method or an explicit cache.” Apply this procedure: State the contract for Choose cached_property only for a stable derived attribute, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The interface communicates lifecycle: cached_property promises reuse of one stored instance value, while other forms expose recomputation or parameters. For the test laboratory, add one near-miss that exposes decorating cheap, volatile or side-effecting work because caching sounds faster. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 3: revision summary. Contrast the rule using a large set of quiz attempts produces one derived mastery score. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Use cached_property when an expensive instance-derived value is read like an attribute and remains valid until deliberate deletion; otherwise prefer property, a method or an explicit cache.” Apply this procedure: State the contract for Choose cached_property only for a stable derived attribute, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The interface communicates lifecycle: cached_property promises reuse of one stored instance value, while other forms expose recomputation or parameters. For the revision summary, add one near-miss that exposes decorating cheap, volatile or side-effecting work because caching sounds faster. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Case 4: reading log. Stress-test the rule using many entries produce a normalized total only when the report asks. State the input grain or object graph, the chapter boundary and the intended output before choosing syntax. Change only one variable, so a wrong prediction has a single plausible cause.

Reasoned route. Begin with “Use cached_property when an expensive instance-derived value is read like an attribute and remains valid until deliberate deletion; otherwise prefer property, a method or an explicit cache.” Apply this procedure: State the contract for Choose cached_property only for a stable derived attribute, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. The expected mechanism is: The interface communicates lifecycle: cached_property promises reuse of one stored instance value, while other forms expose recomputation or parameters. For the reading log, add one near-miss that exposes decorating cheap, volatile or side-effecting work because caching sounds faster. The answer is complete only when it says why the near-miss fails and how the corrected model transfers to a different project without relying on the original variable names.

Diagnostic route

  • Model check: ask the learner to draw or list the exact rows, fields, references, paths or states involved.
  • Boundary check: create the smallest input that triggers decorating cheap, volatile or side-effecting work because caching sounds faster.
  • Evidence check: separate a printed value from identity, ordering, ownership, type or repository state.
  • Repair check: use the reversible procedure “State the contract for Choose cached_property only for a stable derived attribute, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence.” and record the first changed observation.
  • Transfer check: repeat the rule in a second context and identify what remains invariant.

A parent does not need to know the final Python functools.cached_property syntax. For this chapter, useful prompts are: “What did you expect from Choose cached_property only for a stable derived attribute?”, “Which state changed first?”, “What evidence tests that prediction?”, and “Can the case exposing decorating cheap, volatile or side-effecting work because caching sounds faster be made smaller?” The learner, not the parent, should supply the technical explanation.

Practice with an explained answer

Question. Build a tiny revision summary with a large set of quiz attempts produces one derived mastery score. Include one ordinary case, one boundary and one deliberate failure caused by decorating cheap, volatile or side-effecting work because caching sounds faster. Predict each result before using a tool, then report the first point where observation differs from prediction.

Answer guide. A strong response starts with the rule: Use cached_property when an expensive instance-derived value is read like an attribute and remains valid until deliberate deletion; otherwise prefer property, a method or an explicit cache. It shows a trace, not only a final value. The ordinary case should demonstrate “The interface communicates lifecycle: cached_property promises reuse of one stored instance value, while other forms expose recomputation or parameters.” The boundary must exercise the same mechanism at an edge, and the deliberate failure must be repaired with: State the contract for Choose cached_property only for a stable derived attribute, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Other data choices are valid when the evidence supports the same causal chain.

Decision and transfer

For Choose cached_property only for a stable derived attribute, separate the documented Python functools.cached_property mechanism from the project policy. State exactly what the technical contract guarantees, then state the project choice about validation, ordering, ownership, performance or recovery. Test whether the same distinction survives one transfer case, and keep stateful experiments disposable and backed up.

Previous chapter . Contents . Next chapter

Parent guide: choose the next useful step

Start with evidence, not a label such as careless. Ask for one prediction and one trace. If the first transition is wrong, rebuild the model. If the model is sound but syntax fails, practise reference use. If routine cases are correct but boundaries fail, vary ties, defaults, unsupported inputs, ownership or missing paths. If explanations transfer, move to a small project.

Keep a weekly record with four lines: concept, prediction, observed difference and next test. Stop when fatigue replaces reasoning. A smaller case tomorrow is more useful than another hour of copying tonight.

Seek specialist help when cause and effect remain invisible after examples are reduced, when accessibility or data-loss implications are unclear, or when an important repository, database or application state may be at risk. Good support should make the learner’s reasoning more independent.

Capstone practice with explained routes

1. revision summary: model, boundary and recovery

Create a small revision summary using a large set of quiz attempts produces one derived mastery score. Combine “The descriptor computes on a missing attribute” with one later chapter. Include an ordinary case, a boundary, a deliberate failure and a recovery. Write the expected state before each operation.

Explained route. Start with: A cached_property descriptor runs its getter only when normal lookup finds no same-named value in the instance dictionary. Apply: State the contract for The descriptor computes on a missing attribute, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Verify: Construction prints only before; the first r.total lookup prints compute and returns 42. Then add a second chapter whose boundary could change the outcome. A complete solution contains the input model, a trace, observed evidence, a correction and one transfer statement. The exact data may differ; the causal chain must be checkable.

2. reading log: model, boundary and recovery

Create a small reading log using many entries produce a normalized total only when the report asks. Combine “Writes can replace the cached value” with one later chapter. Include an ordinary case, a boundary, a deliberate failure and a recovery. Write the expected state before each operation.

Explained route. Start with: Unlike an ordinary read-only property, cached_property permits assignment; the new instance value then takes precedence. Apply: State the contract for Writes can replace the cached value, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Verify: The printed value is 99 and the getter is not called again for that read. Then add a second chapter whose boundary could change the outcome. A complete solution contains the input model, a trace, observed evidence, a correction and one transfer statement. The exact data may differ; the causal chain must be checkable.

3. science dataset: model, boundary and recovery

Create a small science dataset using measurements produce one expensive calibration summary. Combine “Returning None still creates a cache entry” with one later chapter. Include an ordinary case, a boundary, a deliberate failure and a recovery. Write the expected state before each operation.

Explained route. Start with: A successful getter result of None is stored like any other value; cached_property does not use None as a missing sentinel. Apply: State the contract for Returning None still creates a cache entry, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Verify: The dictionary contains value: None, so later reads do not call the getter merely because the value is None. Then add a second chapter whose boundary could change the outcome. A complete solution contains the input model, a trace, observed evidence, a correction and one transfer statement. The exact data may differ; the causal chain must be checkable.

4. CCA roster: model, boundary and recovery

Create a small CCA roster using participants produce an index used repeatedly after construction. Combine “Mutable source fields require invalidation policy” with one later chapter. Include an ordinary case, a boundary, a deliberate failure and a recovery. Write the expected state before each operation.

Explained route. Start with: Changing inputs after the first read does not invalidate the stored value; the application must delete, recompute or prevent the mutation. Apply: State the contract for Mutable source fields require invalidation policy, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Verify: The reads produce 12, 12 and then 40 because only deletion reopens computation. Then add a second chapter whose boundary could change the outcome. A complete solution contains the input model, a trace, observed evidence, a correction and one transfer statement. The exact data may differ; the causal chain must be checkable.

5. budget worksheet: model, boundary and recovery

Create a small budget worksheet using transactions produce a category total until source rows change. Combine “Metaclass instances expose read-only namespace proxies” with one later chapter. Include an ordinary case, a boundary, a deliberate failure and a recovery. Write the expected state before each operation.

Explained route. Start with: A type object’s __dict__ is a read-only mapping proxy, so cached_property is not a general cache for metaclass attributes. Apply: State the contract for Metaclass instances expose read-only namespace proxies, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Verify: Access cannot store label through the read-only class namespace proxy; a different metaclass-level design is required. Then add a second chapter whose boundary could change the outcome. A complete solution contains the input model, a trace, observed evidence, a correction and one transfer statement. The exact data may differ; the causal chain must be checkable.

6. homework graph: model, boundary and recovery

Create a small homework graph using tasks produce a dependency order for several displays. Combine “Synchronize the critical work when needed” with one later chapter. Include an ordinary case, a boundary, a deliberate failure and a recovery. Write the expected state before each operation.

Explained route. Start with: If duplicate execution is harmful, locking belongs inside the getter or around access with a second check appropriate to the application. Apply: State the contract for Synchronize the critical work when needed, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Verify: The application, not cached_property, owns the required synchronization and side-effect policy. Then add a second chapter whose boundary could change the outcome. A complete solution contains the input model, a trace, observed evidence, a correction and one transfer statement. The exact data may differ; the causal chain must be checkable.

7. library catalogue: model, boundary and recovery

Create a small library catalogue using records produce a search index after first request. Combine “Explicit caches can model arguments and invalidation” with one later chapter. Include an ordinary case, a boundary, a deliberate failure and a recovery. Write the expected state before each operation.

Explained route. Start with: lru_cache, a private sentinel, a versioned key or an explicit recompute method can be clearer when results depend on arguments or several changing sources. Apply: State the contract for Explicit caches can model arguments and invalidation, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Verify: The explicit method makes mutation, ownership and refresh visible when automatic attribute caching would hide them. Then add a second chapter whose boundary could change the outcome. A complete solution contains the input model, a trace, observed evidence, a correction and one transfer statement. The exact data may differ; the causal chain must be checkable.

8. test laboratory: model, boundary and recovery

Create a small test laboratory using call counts, deletion, mutation and threads reveal the cache lifecycle. Combine “The result is written into the instance” with one later chapter. Include an ordinary case, a boundary, a deliberate failure and a recovery. Write the expected state before each operation.

Explained route. Start with: After a successful getter call, cached_property stores the returned value under the property name in the instance dictionary. Apply: State the contract for The result is written into the instance, predict one ordinary case and one boundary, run the smallest disposable test, then explain the earliest difference between prediction and evidence. Verify: The second dictionary contains total mapped to 42, making the cache visible and per instance. Then add a second chapter whose boundary could change the outcome. A complete solution contains the input model, a trace, observed evidence, a correction and one transfer statement. The exact data may differ; the causal chain must be checkable.

Frequently asked questions

How long should a practice session be?

Use one complete prediction–observation–explanation cycle while attention remains good. Ten to twenty focused minutes can be enough.

Should every option or function be memorised?

No. Memorise the governing distinctions and practise retrieving the official reference. Understanding means predicting and explaining, not reciting a parameter list.

What if the result is correct but the explanation is weak?

Treat it as partial success. Ask for a trace and change one boundary. A reliable model survives controlled variation.

Is the shortest solution the best?

Not automatically. Prefer the solution whose semantics, failure modes and maintenance cost are easiest to justify for the actual project.

When should official documentation be used?

Use it whenever syntax, supported types, SQL dialect behaviour or Git version details matter. Primary documentation settles the current contract.

How can a parent help without technical expertise?

Ask what was predicted, where the first difference appeared, what evidence matters and which smaller example could isolate it.

How do we test transfer?

Change the context, vocabulary and one boundary. Require the learner to identify the invariant before using a tool.

What should be saved after practice?

Keep the corrected rule, one trace, one boundary case and the next question. Avoid storing pages of unexplained output.

Can these exercises replace backups?

No. Use disposable examples and proper backups. Learning should not endanger schoolwork, repositories or personal data.

What counts as mastery?

The learner can predict, verify, diagnose, recover and justify a choice across more than one context, while knowing when to consult the current reference.

Official and supporting references

Return to contents

Continue from here: Start Here · Tuition · Education · Pathways · Parenting 101 · All Site Routes

eduKate Punggol

Contact

83 Punggol Central, Singapore 828761

edu|Kate Bukit Timah

8 Fourth Avenue, Singapore 268674

By Appointment +65 8823 1234
admin@edukatesg.com

Email Us

When a child finally understands, school becomes less frightening and the future opens wider. Email us for the latest schedules and fees.

← 返回

感谢您的回复。 ✨

了解 eduKate Punggol 的更多信息

立即订阅以继续阅读并访问完整档案。

继续阅读