main and dependent objects

Discussion in 'Python' started by andrea crotti, Sep 13, 2012.

  1. I am in a situation where I have a class Obj which contains many
    attributes, and also contains logically another object of class
    Dependent.

    This dependent_object, however, also needs to access many fields of the
    original class, so at the moment we did something like this:


    class Dependent:
    def __init__(self, orig):
    self.orig = orig

    def using_other_attributes(self):
    print("Using attr1", self.orig.attr1)


    class Obj:
    def __init__(self):
    self.attr1 = "attr1"
    self.attr2 = "attr2"
    self.attr3 = "attr3"

    self.dependent_object = Dependent(self)


    But I'm not so sure it's a good idea, it's a bit smelly..
    Any other suggestion about how to get a similar result?

    I could of course passing all the arguments needed to the constructor of
    Dependent, but it's a bit tedious..


    Thanks,
    Andrea
     
    andrea crotti, Sep 13, 2012
    #1
    1. Advertising

  2. Am 13.09.2012 14:51, schrieb andrea crotti:
    > I am in a situation where I have a class Obj which contains many
    > attributes, and also contains logically another object of class
    > Dependent.
    >
    > This dependent_object, however, also needs to access many fields of the
    > original class, so at the moment we did something like this:

    [...]
    > I could of course passing all the arguments needed to the constructor of
    > Dependent, but it's a bit tedious..


    Jean-Michel already asked a good question, i.e. whether those two
    classes should be separate at all. I'll ask a similar question: Can't
    the shared data be put into a third, separate class? That way passing
    all the needed arguments wouldn't be tedious any more. Also, it makes
    clear that both outer and inner class depend on common data, but that
    the inner class doesn't depend on the outer beyond that.

    Now, just to get at least something Python-specific into this, you could
    override the __getitem__ of the inner class and transparently look up
    the item in the outer class if the inner class doesn't have it.

    Uli
     
    Ulrich Eckhardt, Sep 13, 2012
    #2
    1. Advertising

  3. andrea crotti

    alex23 Guest

    On Sep 13, 10:52 pm, andrea crotti <> wrote:
    > I am in a situation where I have a class Obj which contains many
    > attributes, and also contains logically another object of class
    > Dependent.
    > But I'm not so sure it's a good idea, it's a bit smelly..


    It's actually a well regarded technique known as composition:
    http://en.wikipedia.org/wiki/Object_composition

    While it has an ostensible focus on game development, I found this
    article to be very good at explaining the concept:
    http://gameprogrammingpatterns.com/component.html
     
    alex23, Sep 14, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. puzzlecracker
    Replies:
    1
    Views:
    542
    James Kanze
    Aug 7, 2008
  2. null hypothesis
    Replies:
    1
    Views:
    300
    Eric Sosman
    Sep 6, 2010
  3. Jean-Michel Pichavant

    Re: main and dependent objects

    Jean-Michel Pichavant, Sep 13, 2012, in forum: Python
    Replies:
    0
    Views:
    190
    Jean-Michel Pichavant
    Sep 13, 2012
  4. andrea crotti

    Re: main and dependent objects

    andrea crotti, Sep 13, 2012, in forum: Python
    Replies:
    0
    Views:
    170
    andrea crotti
    Sep 13, 2012
  5. Jean-Michel Pichavant

    Re: main and dependent objects

    Jean-Michel Pichavant, Sep 13, 2012, in forum: Python
    Replies:
    0
    Views:
    185
    Jean-Michel Pichavant
    Sep 13, 2012
Loading...

Share This Page