top | item 45403677

(no title)

alfons_foobar | 5 months ago

I 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

discuss

order

_flux|5 months ago

Well, 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|5 months ago

Fair point!

Maybe I am just a bit burned by this particular example I ran into (where this pattern should IMO not have been used).