Informações

Prazo de entrega 11/05/2026 18:15:00
Limite de submissão No limitation

Entrar

Pyttern SubPattern: Getter Setter

The Sub-pattern Paradigm

Last time, we used Compound Patterns to combine separate .pyt files using folder structures (AND, OR, NOT). Today, we introduce Sub-patterns (.myt files).

A sub-pattern allows you to define a complex rule in a single file by breaking it down into named blocks. This is more powerful because it allows variables (like class names) to be shared across different blocks, ensuring they refer to the same code element.

Why this is better:
  • Variable Unification: The names wildcards (?{name}) are guaranteed to be the same in all blocks as long as they are an input to the sub-pattern.
  • Readability: The entire design requirement is visible in one place.
  • Logic: The $& at the top indicates that both blocks must match for the whole sub-pattern to be a "Match" (equivalent to the AND folder). And the $| allows for multiple ways to satisfy the return condition (equivalent to the OR folder).
Limitations:
  • no NOT operator in sub-patterns, so you can't express "this block must NOT be present" within a sub-pattern. You would need to handle that logic separately in the main pattern file using the NOT folder.
  • Might be harder to debug due to the novelty of the approach and the lack of extensive debugging website support.

More information can be found on the Pyttern repository.

Scenario

You want to ensure students are properly encapsulating data. You need to find classes that define a variable in __init__ and provide both a getter and a setter for it.

Task

Create a sub-pattern file named getset.myt. The sub-pattern should contains all the code needed to match the getter setter pattern. All three methods can be in any order and with any number of methods in between.

Directions:

  • Use the sub-pattern header: $&GetSet(?getter, ?setter, ?var)
  • You need three blocks:
    1. Init: An __init__ method where self.?var is assigned a value.
    2. Get: A method named ?getter that returns self.?var.
    3. Set: A method named ?setter that takes a new value and assigns it to self.?var.

Example:

class Foo:
    def __init__(self):
        self.bar = 0

    def set_bar(self, bar):
        self.bar = bar

    def get_bar(self):
        return self.bar

Questão 1: Pyttern
Questão 2: sub-pattern