Python:“超级”对象没有属性“attribute_name”;

发布于 2024-11-09 06:48:22 字数 764 浏览 0 评论 0原文

我正在尝试从基类访问变量。这是父类:

class Parent(object):
    def __init__(self, value):
        self.some_var = value

这是子类:

class Child(Parent):
    def __init__(self, value):
        super(Child, self).__init__(value)

    def doSomething(self):
        parent_var = super(Child, self).some_var

现在,如果我尝试运行此代码:

obj = Child(123)
obj.doSomething()

我会得到以下异常:

Traceback (most recent call last):
  File "test.py", line 13, in <module>
    obj.doSomething()
  File "test.py", line 10, in doSomething
    parent_var = super(Child, self).some_var
AttributeError: 'super' object has no attribute 'some_var'

我做错了什么?在 Python 中从基类访问变量的推荐方法是什么?

I am trying to access a variable from the base class. Here's the parent class:

class Parent(object):
    def __init__(self, value):
        self.some_var = value

And here's the child class:

class Child(Parent):
    def __init__(self, value):
        super(Child, self).__init__(value)

    def doSomething(self):
        parent_var = super(Child, self).some_var

Now, if I try to run this code:

obj = Child(123)
obj.doSomething()

I get the following exception:

Traceback (most recent call last):
  File "test.py", line 13, in <module>
    obj.doSomething()
  File "test.py", line 10, in doSomething
    parent_var = super(Child, self).some_var
AttributeError: 'super' object has no attribute 'some_var'

What am I doing wrong? What is the recommended way to access variables from the base class in Python?

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

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

发布评论

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

评论(3

清秋悲枫 2024-11-16 06:48:22

基类的 __init__ 运行后,派生对象具有在那里设置的属性(例如 some_var),因为它与中的 self 是同一对象。派生类'__init__。您可以而且应该在任何地方使用 self.some_var 。 super 用于访问基类中的内容,但实例变量(如名称所示)是实例的一部分,而不是该实例的类的一部分。

After the base class's __init__ ran, the derived object has the attributes set there (e.g. some_var) as it's the very same object as the self in the derived class' __init__. You can and should just use self.some_var everywhere. super is for accessing stuff from base classes, but instance variables are (as the name says) part of an instance, not part of that instance's class.

等数载,海棠开 2024-11-16 06:48:22

属性 some_var 在父类中不存在。

当您在 __init__ 期间设置它时,它是在您的 Child 类的实例中创建的。

The attribute some_var does not exist in the Parent class.

When you set it during __init__, it was created in the instance of your Child class.

会发光的星星闪亮亮i 2024-11-16 06:48:22

我得到了同样的错误,这是一个愚蠢的错误

class one:
    def __init__(self):
        print("init")
    def status(self):
        print("This is from 1")
    

这是我的父类

class two:
    def __init__(self):
        print("init")
    def status(self):
        super().status()
        print("This is from 2")
    

子类

a = one()
a.status()

b = two()
b.status()

这是我得到同样错误的

init
This is from 1
init
Traceback (most recent call last):
  File "<string>", line 20, in <module>
  File "<string>", line 12, in status
AttributeError: 'super' object has no attribute 'status'
> 

问题是,我在声明第二类后没有输入参数,
“二类:”应为“二类(一)”
所以解决方案是。

class two(one):
def __init__(self):
    print("init")
def status(self):
    super().status()
    print("This is from 2")

I got same error, and it was a silly mistake

class one:
    def __init__(self):
        print("init")
    def status(self):
        print("This is from 1")
    

This was my parent class

class two:
    def __init__(self):
        print("init")
    def status(self):
        super().status()
        print("This is from 2")
    

This was the child class

a = one()
a.status()

b = two()
b.status()

I was getting same error

init
This is from 1
init
Traceback (most recent call last):
  File "<string>", line 20, in <module>
  File "<string>", line 12, in status
AttributeError: 'super' object has no attribute 'status'
> 

The problem was, I was not entering argument after declaring class two,
"class two:" should be "class two(one)"
so the solution was.

class two(one):
def __init__(self):
    print("init")
def status(self):
    super().status()
    print("This is from 2")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文