The python implementation of the "relationships between classes".

Chris Angelico rosuav at gmail.com
Thu Nov 10 08:26:37 EST 2011


On Fri, Nov 11, 2011 at 12:15 AM, Jerry Zhang <jerry.scofield at gmail.com> wrote:
> For example, in composition model, the container object may be responsible
> for the handling of embedded objects' lifecycle. What is the python pattern
> of such implementation?

Here's an example:

class Test(object):
  def __init__(self):
    self.lst=[1,2,3]
    self.map={"foo":12,"bar":34}

This is a container that has a list and a dictionary in it. When an
instance of the container is destroyed, its list and dictionary will
be destroyed too (assuming nobody's made other references to them).
You don't need to code anything explicitly for that.

Chris Angelico



More information about the Python-list mailing list