Python:“尚未定义”类名作为默认参数

发布于 2024-10-10 23:07:52 字数 601 浏览 3 评论 0原文

我有一个“class1”,它必须能够创建一个对象 不同的类名。类名作为称为“friend”的参数传递。 我希望“friend”参数默认为名为“class2”的类名。

另外,我需要对“class2”类有相同的行为。 所以“class2”应该有“class1”作为默认的朋友参数:

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

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

class1()
class2()

现在我收到以下错误消息:

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

当然,我不能在 class1 之前定义 class2 因为 这会导致类似的错误:“class1”未定义。 你知道解决办法吗?

非常感谢您的帮助!

亨利

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高速公鹿 2024-10-17 23:07:52

您可以将其推到稍后:

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

编辑:实际上,不要这样做。它将创建一个 class2 实例,该实例创建一个 class1 实例,该实例创建一个 class2 实例,等等。也许您确实想传递一个实例而不是要实例化的类:

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

对于 class2 也是如此。虽然不太灵活,但非常简单。如果你真的想要灵活性,你可以这样做:

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

这可以通过继承或元类来简化,但你可能明白了。

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.

护你周全 2024-10-17 23:07:52

即使您解决了NameError,您也会遇到另一个问题——即您试图创建递归数据结构。 class1 的每个实例都尝试<​​em>创建一个class2 的实例,该实例同样尝试创建另一个class1 实例,等等,无限循环(实际上只有到你得到一个RuntimeError:超出最大递归深度)。

在不了解您实际想要做什么的情况下,这里有一个简单的解决方案:

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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文