Search code examples
pythonclassparametersargumentsdefault-parameters

Python: Having "not-yet-defined" class-names as default-arguments


I have a "class1" which must be able to create an object of a different class-name. The class-name is passed as an argument called "friend". I want the "friend"-argument to default to a class-name called "class2".

Additionally I need to have the same behavior for the class "class2". So "class2" should have "class1" as a default friend-argument:

class class1():
 def __init__(self, friend = class2):
  self.friendInstance = friend()

class class2():
 def __init__(self, friend = class1):
  self.friendInstance = friend()

class1()
class2()

Now i get the following error-message:

    def __init__(self, friend = class2):
NameError: name 'class2' is not defined

Of course, i can't define class2 before class1 because this would result in a similar error: "class1" is not defined. Do you know a solution?

Thanks a lot for your help!

Henry


Solution

  • You can push it to later:

    class class1(object):
        def __init__(self, friend=None):
            if friend is None:
                friend = class2
            self.friendInstance = friend()
    

    Edit: Actually, don't do that. It will create a class2 instance that creates a class1 instance that creates a class2 instance, etc. Maybe you really want to pass an instance in instead of the class to be instantiated:

    class class1(object):
        def __init__(self, friend=None):
            if friend is None:
                self.friendInstance = class2(self)
            else:
                self.friendInstance = friend
    

    and likewise for class2. That isn't as flexible, but it's pretty simple. If you really want flexibility, you can do something like this:

    class class1(object):
        def __init__(self, friend=None, friendClass=None):
            if friend is None:
                self.friendInstance = (class2 if friendClass is None else friendClass)(self)
            else:
                self.friendInstance = friend
    
    class class2(object):
        def __init__(self, friend=None, friendClass=class1):
            if friend is None:
                self.friendInstance = friendClass(self)
            else:
                self.friendInstance = friend
    

    That could be simplified with inheritance or metaclasses, but you probably get the idea.