Accessing parent objects

Ian Kelly ian.g.kelly at gmail.com
Sun Mar 25 11:31:06 EDT 2018


On Sun, Mar 25, 2018 at 9:01 AM, Jugurtha Hadjar
<jugurtha.hadjar at gmail.com> wrote:
>> On 03/25/2018 03:25 PM, Terry Reedy wrote:
>> None.foo will raise AttributeError.
>>>> Right.. As I said, I tried to assume as little as possible about OP's code
> and namespace. Didn't want to include C1 in __init__ signature because I
> wasn't sure it was available in the namespace.
>> It is easy to address, though:
>> Example A:
>> class C2(object):
> def __init__(self, parent=C2):
> self.parent = parent

This doesn't work as written (try it!)
> Furthermore, having a positional argument will not save us. We can still
> break the code if we do the following:
>> class C2(object):
> def __init__(self, parent):
> self.parent = parent
> def foo(self):
> self.parent.foo()
>> c1 = C1()
> c2 = C2(None)
> c2.foo()
>> Making it positional didn't fix our wickedness.

The difference is that in this case the fault is on the caller for
passing a nonsense value. In the original the fault is on the class
author for providing a useless default and implying that the argument
is not required.


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /