an oop question

Julieta Shem jshem at yaxenu.org
Thu Nov 3 08:29:20 EDT 2022


Greg Ewing <greg.ewing at canterbury.ac.nz> writes:

> On 3/11/22 1:37 pm, Julieta Shem wrote:
>> The code for computing the length of a Pair (which is really a linked
>> list) happens to be the same for computing the length of a Stack.
>
> I would question whether that should be a method of Pair at all,
> since it's not the length of the pair itself, but the length of
> a structure built out of pairs.

You make a lot of sense.

> But in any case, the way to do this in a conceptually clean way
> is for the length method of Stack to call whatever it is that
> computes the length of a linked list of Pairs. This is what
> the term "delegation" refers to.

Thank you for the example.  I agree with you.  Perhaps I can reduce the
class Pair to just being a pair as we know it, made of two things, then
we create a class called Sequence, which is really a composition of
Pairs, comes with a length method and we derive Stack from it.


More information about the Python-list mailing list