On 3/25/2018 7:42 AM, Jugurtha Hadjar wrote:
class C2(object):
def __init__(self, parent=None):
self.parent = parent
Since parent is required, it should not be optional.
def foo(self):
print("I am {self.__class__.__name__} foo".format(self=self))
self.parent.foo()
None.foo will raise AttributeError.
class C1(object):
def __init__(self, child_class=None):
self.child = child_class(parent=self)
Ditto. None() will raise TypeError
If your intent is to force passing parent/child_class by name rather
than by position, use *, as in
def __init__(self, *, child_class):
def foo(self):
print("I am {self.__class__.__name__} foo".format(self=self))
c1 = C1(child_class=C2)
c1.child.foo() # I am C2 foo
# I am C1 foo
--
Terry Jan Reedy
--
https://mail.python.org/mailman/listinfo/python-list