osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Accessing parent objects


On 03/25/2018 11:57 AM, D'Arcy Cain wrote:
>
>> Something like this:
>>
>> class C1(object):
> ...
>> class C2(object):
>>  ??? def __init__(self, parent=None):
>>          self.parent = parent
> Perhaps your email client is collapsing leading spaces but that isn't
> what I wrote.  The C2 class is supposed to be a member of C1.  That's
> why I called it as O1.C2() instead of just C2().  I was hoping that by
> doing so that the data in O1 would somehow be available without having
> to explicitly pass it as an argument.

It's not my mail client, this was my proposed code in which C2 is 
defined *outside* of C1, then you inject one or the other (can't decide 
for you as it depends on what you're trying to do).
In other words, it's not an indentation error.

> However, I would have made the parameter required.  I don't see the
> point of calling another method to add it in later.  Of course, if I do
> that there's no point in making C2 part of C1.  What you wrote would be
> just fine and obvious.
>

I tend to use keyword arguments a lot because it gives me a lot of 
freedom. Just a perk: I can do things like change the API and I'd only 
have to catch the call, issue a deprecation warning, and then use the 
new API instead of having the code be rigid.

-- 
~ Jugurtha Hadjar,