Composition - Mon, Oct 14

Linked Lists

Linked lists are wither empty or a first value and the rest of the linked list.

class Link:
    empty = ()

    def __init__(self, first, rest=empty):
        assert rest is Link.empty or isinstance(rest, Link)
        self.first = first
        self.rest = rest

Property & Setter Methods

The @property decorator on a method designates that it will be called whenever it is looked up on an instance.

class Link:
    
    @property
    def second(self):
        return self.rest.first

A @<attribute>.setter decorater on a mehod designates that it will be called whenever that attribute is assigned. <attribute> must be an existing property method.

class Link:

    @second.setter
    def second(self, value):
        self.rest.first = value