Python: having "not yet defined" class names as default arguments

I have a "class1" that should be able to create an object with a different class name. The class name is passed as an argument called "friend". I want friend -argument to be called class2 by default.

Also, I need to have the same behavior for class "class2". Thus, "class2" should have "class1" as the 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 cannot define class2 before class1, because it will lead to a similar error: "class1" is not defined. Do you know a solution?

Many thanks for your help!

Henry

+4
source share
2 answers

You can click it later:

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

Edit: Actually, do not do this. It will create an instance of class2, which creates an instance of class1, which creates an instance of class2, etc. Perhaps you really want to pass an instance instead of the class you are creating:

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

and similarly for class 2. It's not that flexible, but it's pretty simple. If you really need 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 

This may be simplified with inheritance or metaclasses, but you are likely to get this idea.

+3
source

Even if you solve NameError , you will come across another, namely that you are trying to create a recursive data structure. Each instance of class1 tries to create an instance of class2 , which also tries to create another class1 instance, etc., etc., ad infinitum (actually only as long as you get RuntimeError: maximum recursion depth exceeded ).

Without knowing a little more about what you are actually trying to do, here is one simple solution:

 class class1(object): def __init__(self, friend=None): if friend is None: friend = class2(self) # create a class2 instance with myself as a friend self.friendInstance = friend class class2(object): def __init__(self, friend=None): if friend is None: friend = class1(self) # create a class1 instance with myself as a friend self.friendInstance = friend print class1() # <__main__.class1 object at 0x00B42450> print class2() # <__main__.class2 object at 0x00B65530> 
+1
source

Source: https://habr.com/ru/post/1334940/


All Articles