1

I am writing a connection-state keeping program based loosely on the "State" example here.

In the example, the class Radio passes a pointer (better term?) to AmState when instancing AmState:

class AmState(State):
    def __init__(self, radio):
    self.radio = radio

...
class Radio(object):
    def __init__(self):
        ...
        self.amstate = AmState(self)

The example stores this pointer (?) in the name 'radio', but what would be the correct Python/OO term for this value? I keep wanting to say "parent", but 'AmState's parent is 'State', not 'radio'.

What is the terminology I should be using here to minimize confusion when documenting my code?

Thanks

JS.
  • 14,781
  • 13
  • 63
  • 75
  • There are plenty of options, but there's nothing wrong with "parent". Note that this isn't a great pattern in general, since you're creating a circular reference for every one of these objects, which makes GC less efficient and will give you headaches if you have any `__del__` methods. – Glenn Maynard Mar 07 '11 at 22:43
  • I disagree with @Glenn Maynard about it being OK to use "parent" because that term is often used to designate the base class (or classes) of a derived class -- which is not what the OP is doing. In fact, that's exactly what I thought the question was going to be about from its title. – martineau Mar 07 '11 at 23:30
  • @martineau: "Parent" is a generic term used in describing hierarchies; classes are just one case where it's used. It's in no way specific to classes. – Glenn Maynard Mar 07 '11 at 23:59
  • Maybe I just don't get it, but I think the State example you linked to is pretty strange. Python doesn't need state objects, we have generators to keep state. – Jochen Ritzel Mar 08 '11 at 00:17
  • @Glenn Maynard: I agree, _technically_ "parent" is correct. But I found myself thinking "parent class" every time I saw 'self.parent'. If I'm getting confused while I'm developing the code, it's only gonna be worse when I (or someone else) comes back later and tries to understand it. Hence the call for recommended alternatives. Basically, the term "parent" is (too) overloaded. :-) – JS. Mar 08 '11 at 01:00
  • If you find that confusing, you're going to have trouble dealing with any code dealing with trees or filesystems. – Glenn Maynard Mar 08 '11 at 02:03
  • My old "file system" was made from trees. I'll be ok. – JS. Mar 08 '11 at 17:50

2 Answers2

2

The usual term for this sort of composition is "owner". And the "better term" you're looking for is "reference".

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • +1: Totally agree with "reference" and the term "owner" make sense since from the code snippet it looks like a `Radio` *has a* `AmState`. – martineau Mar 07 '11 at 23:41
1

I suppose GOF Design Patterns is the nearest thing to canon, and it uses 'context'.

fizzer
  • 13,551
  • 9
  • 39
  • 61
  • In the GOF *State* pattern a `Context` -- corresponding to `Radio` in the code snippet -- has a reference to a `State`, but they don't have reciprocal ones, unlike the code snippet. Based on other patterns in the book, a good name for that kind of reference might be "owner". Some others that come to mind, depending on the specific application, might be "controller" or "container". – martineau Mar 08 '11 at 21:47