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 fo开发者_如何学编程r 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
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.
Even if you solve the NameError
, you'll run into another -- namely that you've attempting to create a recursive data-structure. Each instance of class1
attempts to create an instance of class2
, which likewise attempts to create another class1
instance, etc, etc, ad infinitum (actually only until you get a RuntimeError: maximum recursion depth exceeded
).
Without knowing a little more about what you're actually trying to do, here's 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>
精彩评论