1

Is there some clever way to do this that works?

class NamedElement:
    def __init__(self, **kwargs):
       self.name = Property(name=kwargs.get('name')) #you're going to regret this


class Property(NamedElement):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

myprop = Property(name='myprop') #don't hit enter!!!

If anyone wants context, I'm interested in trying to build a UML model in Python .. UML models itself with UML so here I am.

14
  • 1
    The base class should not depend on any particular subclass existing. It seems like there should probably be some other subclass of NameElement that has a Property-valued attribute. Commented Dec 6, 2022 at 20:43
  • 1
    self.name = kwargs.get('name') would avoid the recursion. past that, no idea what you are trying to do. this is one of the cases where articulating your expected result might be more informative than your code. what do you intend your API/data structures to look like? Commented Dec 6, 2022 at 21:11
  • 1
    I did overlook the infinite loop. But a parent should not depend any particular subclass in any case. (The infinite loop could be avoided, for instance, by having the caller create Property instances and passing them as arguments to NameElement.) Commented Dec 6, 2022 at 21:15
  • 1
    It's seems more likely that NameElement and Property should both be subclasses of some other parent, with a Property not being a kind of NamedElement, but both classes being examples of some other (abstract) entity. Commented Dec 6, 2022 at 22:06
  • 1
    The infinite loop is only logical because you created a datastructure of infinite depth and initializing this causes infiite looping. myprop is a NamedElement which has an attribute name which is a NamedElement which has an attribute name which is a NamedElement which ... Commented Dec 6, 2022 at 22:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.