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


Groups > comp.lang.python > #29028

main and dependent objects

Date 2012-09-13 13:51 +0100
Subject main and dependent objects
From andrea crotti <andrea.crotti.0@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.601.1347540680.27098.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

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

csiph-web