在python中使用嵌套类中的参数调用父方法

在python中使用嵌套类中的参数调用父方法,python,Python,上面的代码按预期工作,但IntelliJ IDEA用此消息警告我 嵌套类的实例。应为父类,而不是类本身 我的代码有问题吗? 谢谢 您没有将输出声明为类方法,因此它预期会被父对象的实例调用。您正在尝试访问作为类方法的实例方法的输出方法。出现此消息是因为第一个参数self是实例对象,因此将类作为第一个参数传递实际上会更改self作为实例对象应该具有的内容。这就是信息试图告诉你的。因此,您实际上应该做的是将该方法作为实例方法调用: class Parent(object): def __ini

上面的代码按预期工作,但IntelliJ IDEA用此消息警告我 嵌套类的实例。应为父类,而不是类本身 我的代码有问题吗?
谢谢

您没有将输出声明为类方法,因此它预期会被父对象的实例调用。

您正在尝试访问作为类方法的实例方法的输出方法。出现此消息是因为第一个参数self是实例对象,因此将类作为第一个参数传递实际上会更改self作为实例对象应该具有的内容。这就是信息试图告诉你的。因此,您实际上应该做的是将该方法作为实例方法调用:

class Parent(object):
    def __init__(self):
        self.text = "abc"
        self.child = self.Child()

    def run(self):
        self.child.task()

    def output(self, param1):
        print(param1)

    class Child(object):
        def __init__(self):
            self.text = "cde"

        def task(self):
            Parent.output(Parent, self.text)  # I get the warning for this line


parent = Parent()
parent.run()
根据您所做的工作,如果您在输出方法中检查self对象的repr和内容,您会得到以下结果:

Parent().output(self.text)
使用此调用方法:Parent.outputpent,self.text


如您所见,您现在有了一个父对象,如果您查看该对象的内容,您将从实例属性中获得所期望的内容

我建议你也把你的代码贴在上面。这里有很多问题可以解决。如果您关注ZachGates的评论,请确保在代码运行之前不要发布它。感谢您的精彩解释!
def output(self, param1):
    print(repr(self))
    print(dir(self))
    print(param1)
<class '__main__.Parent'>
[
    'Child', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', 
    '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', 
    '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', 
    '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', 
    '__subclasshook__', '__weakref__', 'output', 'run'
]
<__main__.Parent object at 0x1021c6320>
[
    'Child', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__',
    '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', 
    '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', 
    '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
    '__weakref__', 'child', 'output', 'run', 'text'
]