Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: main and dependent objects

Thread Tools

Re: main and dependent objects

Jean-Michel Pichavant
Posts: n/a
----- Original Message -----
> 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
> --

Nothing shocking right here imo. It looks like a classic parent-child implementation.
However it seems the relation between Obj and Dependent are 1-to-1. Since Dependent need to access all Obj attributes, are you sure that Dependent and Obj are not actually the same class ?

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
main and dependent objects andrea crotti Python 2 09-14-2012 02:05 AM
Re: main and dependent objects Jean-Michel Pichavant Python 0 09-13-2012 03:22 PM
Re: main and dependent objects andrea crotti Python 0 09-13-2012 02:59 PM
non-dependent vs. dependent template names puzzlecracker C++ 1 08-07-2008 07:42 AM