Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29028 > unrolled thread

main and dependent objects

Started byandrea crotti <andrea.crotti.0@gmail.com>
First post2012-09-13 13:51 +0100
Last post2012-09-13 19:05 -0700
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  main and dependent objects andrea crotti <andrea.crotti.0@gmail.com> - 2012-09-13 13:51 +0100
    Re: main and dependent objects Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-09-13 17:05 +0200
    Re: main and dependent objects alex23 <wuwei23@gmail.com> - 2012-09-13 19:05 -0700

#29028 — main and dependent objects

Fromandrea crotti <andrea.crotti.0@gmail.com>
Date2012-09-13 13:51 +0100
Subjectmain and dependent objects
Message-ID<mailman.601.1347540680.27098.python-list@python.org>
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

[toc] | [next] | [standalone]


#29039

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2012-09-13 17:05 +0200
Message-ID<da99i9-jpt.ln1@satorlaser.homedns.org>
In reply to#29028
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

[toc] | [prev] | [next] | [standalone]


#29098

Fromalex23 <wuwei23@gmail.com>
Date2012-09-13 19:05 -0700
Message-ID<455b699d-1439-4105-829d-fe3499110685@k13g2000pbq.googlegroups.com>
In reply to#29028
On Sep 13, 10:52 pm, andrea crotti <andrea.crott...@gmail.com> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web