简体   繁体   English

AttributeError:“超级”对象没有属性

[英]AttributeError: 'super' object has no attribute

I wrote the following code. 我写了下面的代码。 When I try to run it as at the end of the file I get this stacktrace: 当我尝试在文件末尾运行它时,得到以下堆栈跟踪:

AttributeError: 'super' object has no attribute do_something


class Parent:
    def __init__(self):
        pass

    def do_something(self, some_parameter, next_parameter):
        if type(some_parameter) is not int:
            raise AttributeError("Some message")
        if type(next_parameter) is not int:
            raise AttributeError("Some message")


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

    def do_something(self, some_parameter, next_parameter):
        super(Parent).do_something(some_parameter, next_parameter)
        return some_parameter + next_parameter


object = Child()
object.do_something(2, 2)

How should I solve this and where did I the mistake in this simply inheritance sample? 我应该如何解决呢?在这个简单的继承示例中,我在哪里犯了错误?

You're passing the wrong argument to super . 您将错误的论点传递给super If you're going to pass arguments at all, they need to be the current class and instance, not the parent class you're expecting to call. 如果您要传递参数,则它们必须是当前类和实例,而不是您希望调用的父类。 Or assuming you're using Python 3, you can skip the arguments completely and the compiler will make it work for you anyway. 或者假设您使用的是Python 3,则可以完全跳过参数,编译器将始终为您工作。 Calling super with one argument is allowed, but it returns an "unbound super object" which is almost never useful. 允许使用一个参数调用super ,但是它会返回一个“从未绑定的超级对象”,这几乎是没有用的。

Change your calls to use one of these styles: 更改您的呼叫以使用以下样式之一:

class Child(Parent):
   def __init__(self):
      super().__init__()   # no arguments is almost always best in Python 3

   def do_something(self, some_parameter, next_parameter):
      super(Child, self).do_something(some_parameter, next_parameter) # name the current class
      return some_parameter + next_parameter 

I'd also note that your type checks in Parent.do_something are rather awkward. 我还要注意,您在Parent.do_something中的类型检查相当尴尬。 Rather than type(some_parameter) is not int , use isinstance(some_parameter, int) (unless you deliberately want to exclude subtypes). 而不是type(some_parameter) is not int ,而应使用isinstance(some_parameter, int) (除非您有意排除子类型)。

You have a few issues here. 您在这里有几个问题。 Firstly there was an indentation error for the parent do_something definition. 首先,父do_something定义存在缩进错误。 This meant that it was defined as a function in and of itself, rather than being a method of the class Parent . 这意味着它被定义为本身的函数,而不是Parent类的方法。

Secondly, class methods should usually have self as their first parameter, as when they are called, the object that they refer to is passed as the first variable. 其次,类方法通常应将self作为其第一个参数,因为当调用它们时,它们所引用的对象将作为第一个变量传递。

Thirdly, when you call super() you do not need to specify what the super is, as that is inherent in the class definition for Child . 第三,当您调用super()时,无需指定super是什么,因为那是Child的类定义中固有的。

Below is a fixed version of your code which should perform as you expect. 下面是代码的固定版本,应能按预期执行。

class Parent:
    def __init__(self):
        pass
    def do_something(self, some_parameter, next_parameter):
        if type(some_parameter) is not int:
            raise AttributeError("Some message")
        if type(next_parameter) is not int:
            raise AttributeError("Some message")
class Child(Parent):
    def __init__(self):
        super(Parent).__init__()
    def do_something(self, some_parameter, next_parameter):
        super().do_something(some_parameter, next_parameter)
        return some_parameter + next_parameter

test = Child()
test.do_something(2, 2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:“超级”对象没有属性“ __getattr__” - AttributeError: 'super' object has no attribute '__getattr__' AttributeError:“超级”对象没有属性“ __getattr __”(我进行了搜索,但无济于事) - AttributeError: 'super' object has no attribute '__getattr__' ( I searched, but to no avail) AttributeError: 'super' 对象在 python 中没有属性 '__getattr__' - AttributeError: 'super' object has no attribute '__getattr__' in python Kivy: AttributeError: 'super' object 没有属性 '__getattr__' - Kivy: AttributeError: 'super' object has no attribute '__getattr__' Kivy AttributeError: 'super' object 没有属性 '__getattr__' - Kivy AttributeError: 'super' object has no attribute '__getattr__' Kivy AttributeError: 'super' object has no attribute '__getattr__' 错误 - Kivy AttributeError: 'super' object has no attribute '__getattr__' error Python Kivy 返回 'AttributeError: 'super' object 没有属性 '__getattr__'' - Python Kivy returning 'AttributeError: 'super' object has no attribute '__getattr__'' AttributeError: 'super' object 在部署时没有属性 'get_params' - AttributeError: 'super' object has no attribute 'get_params' while deploying Python Keras ImageDataGenerator:AttributeError:'super'对象没有属性'init' - Python Keras ImageDataGenerator: AttributeError: 'super' object has no attribute 'init' AttributeError:“超级”对象没有属性“ compare_files” - AttributeError: 'super' object has no attribute 'compare_files'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM