3 ms·
> If the type is a class with methods, then this method doesn't work Use typing.Self
by Too 1y ago
> If the type is a class with methods, then this method doesn't work
Use typing.Self
- _flux 1y agoI meant if you have two classes that need to refer to each other. But good pointer anyway, I hadn't noticed it, thanks!
- alfons_foobar 1y agoI ran into some code recently where this pattern caused me so much headache - class A has an attribute which is an instance of class B, and class B has a "parent" attribute (which points to the instance of class A that class B is an attribute of): class Foo: def __init__(self, bar): self.bar = bar class Bar: def __init__(self, foo): self.foo = foo Obviously both called into each other to do $THINGS... Pure madness. So my suggestion: Try not to have interdependent classes :D
- _flux 1y agoWell, at times having a parent pointer is rather useful! E.g. a callback registration will be able to unregister itself from everywhere where it has been registered to, upon request. (One would want to use weak references in this case.)
- alfons_foobar 1y agoFair point! Maybe I am just a bit burned by this particular example I ran into (where this pattern should IMO not have been used).